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

  • SEARCH
  • Home
  • 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 7011039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:04:35+00:00 2026-05-27T22:04:35+00:00

I have 2 json data from nosql . first match if the search word

  • 0

I have 2 json data from nosql. first match if the search word match in $array1, get the item number then put item number into $array2, get the price of custom search require. But my code cause Invalid argument supplied for foreach() in
foreach($json2[$num] as $data2)

$str = 'paper';
$array1 = '[{"a":"1","b":"book"},{"a":"2","b":"paper"}]';  
$array2 = '[{"1":["17.00","22.00"]},{"2",["4.50","6.00"]}]';
$json1 = json_decode($array1);
$json2 = json_decode($array2,true);
foreach($json1 as $data1){
    if(preg_match('#'.$data1->b.'#',$str,$match)){
        $num = $data1->a; // $num = 2
    }
}
foreach($json2[$num] as $data2){
    foreach($data2 as $newdata){
        echo $newdata.'<br />'; // 4.50, 6.00
    }
}
  • 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-27T22:04:35+00:00Added an answer on May 27, 2026 at 10:04 pm

    First off your JSON for $array2 is not valid. It should be:

    [{"1":["17.00","22.00"]},{"2":["4.50","6.00"]}]
    -----------------------------^
    This should be a ":", not a ","
    

    Your JSON is actually an array of objects (arrays). var_dump($json2); shows this.

    array(2) {
      [0]=>
      array(1) {
        [1]=>
        array(2) {
          [0]=>
          string(5) "17.00"
          [1]=>
          string(5) "22.00"
        }
      }
      [1]=>
      array(1) {
        [2]=>
        array(2) {
          [0]=>
          string(4) "4.50"
          [1]=>
          string(4) "6.00"
        }
      }
    }
    

    You’re gonna need to loop over it like this:

    foreach($json2 as $data2){
        if(array_key_exists($num, $data2)){
          $data2 = $data2[$num];
          foreach($data2 as $newdata){
              echo $newdata.'<br />'; // 4.50, 6.00
          }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website where an ajax call will get some Json data from
I have been searching for a way to get the json data from a
I have an external JSON file (data.json) and would like to get data from
I have followed intructions to get json data from vimeo with the follwoing code:
Using the Twitter Search API I have attempted to download JSON data from a
I have a web service that queries data from this json file, but I
I have an issue regarding to auto populating a select dropdown from jQuery/JSON data
I have an application which obtains data in JSON format from one of our
I have a JSON data like this: { hello: { first:firstvalue, second:secondvalue }, hello2:
I have some JSON data coming from a service. I don't know the exact

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.