Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7161663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:36:16+00:00 2026-05-28T13:36:16+00:00

Having this php code: $data[‘items’] = array(‘cars’, ‘bikes’, ‘trains’); $data[‘title’] = $parameters[‘title’]; foreach ($searchResults

  • 0

Having this php code:

$data['items'] =  array('cars', 'bikes', 'trains');
$data['title']   = $parameters['title'];

foreach ($searchResults as $key => $value)
{
    switch ($key)
    {
        case "_cars":
        foreach ($searchResults['_cars']['items'] as $car)
        {
            preg_match('@video/([^_]+)_([^/]+)@', $car['url'], $match);
            $url = $match[1].'/'.$match[2];
            $url = base_url().'video/'.substr($url,0,1).'d'.substr($url,1);

            $data['data']['car']['url']   = $url;
            $data['data']['car']['title'] = $car['title'];
            $data['data']['car']['img']   = $car['thumbnail_medium_url'];
        }
        break;
        // ................

How can I fix this, or what I am doing wrong because $['data']['car'][...] returns only 1 item for url, title and img outside case "_cars": foreach... but inside it does returns all the data.

Edit:
but I wonder why doing print_r($data) inside the foreach ($searchResults['_cars']['items']... loop returns all data and outside that foreach only 1?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:36:17+00:00Added an answer on May 28, 2026 at 1:36 pm

    I’m not quite sure what you’re wanting, but at first glance, it looks like each iteration is overwriting the actual value of the array key:

    $data['data']['car']['url'] = $url; // this is overwritten each time
    

    You need to create an iterator and use that:

    $i = 0;
    foreach ($searchResults['_cars']['items'] as $car)
    {
      preg_match('@video/([^_]+)_([^/]+)@', $car['url'], $match);
      $url = $match[1].'/'.$match[2];
      $url = base_url().'video/'.substr($url,0,1).'d'.substr($url,1);
    
      $data['data']['car'][$i]['url']   = $url;
      $data['data']['car'][$i]['title'] = $car['title'];
      $data['data']['car'][$i]['img']   = $car['thumbnail_medium_url'];
      $i++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is follow up post where I'm having a problem where my php code
I'm having trouble with this PHP script where I get the error Fatal error
We're having this discussion here. Which is better and why? 1) a php file
I'm a rookie designer having a few troubles with this page: http://www.resolvegroup.co.nz/javasurvey.php There are
Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here
Having this code... var b = new ReadOnlyCollection<int>(new[] { 2, 4, 2, 2 });
The data for the SQL db used in my PHP code comes from an
I'm having a problem with some PHP code I'm trying to use. I keep
Here is the code. It seems to be having an issue in PHP 5.
I am trying to insert data in table in mysql database through php code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.