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 7874057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:41:50+00:00 2026-06-03T02:41:50+00:00

Here (given below) is some very simple php parsing multidimensional array stuff I am

  • 0

Here (given below) is some very simple php parsing multidimensional array stuff I am doing. I am just searching for ‘highlighting’ key and then storing some key value pairs in another array. Is there any better way to achieve this (I mean with respect to performance), rather than have n foreach loops to get to what you want.

$json_O=json_decode(file_get_contents($url),true);
     foreach($json_O as $section1=>$items1){
        if($section1==highlighting){
            foreach($items1 as $section2=>$items2){
                    $key=$section2;
                    foreach($items2 as $section3=>$items3){
                        foreach ($items3 as $section4=>$items4){
                            $value=$items4;
                            $found[]=array('Key' => $key, 'Value' => $value);

Here is a sample php object I am trying to parse:

Array
(
    [responseHeader] => Array
        (
            [status] => 0
            [QTime] => 3
            [params] => Array
                (
                    [indent] => on
                    [start] => 0
                    [q] => russian
                    [fragsize] => 40
                    [hl.fl] => Data
                    [wt] => json
                    [hl] => on
                    [rows] => 8
                )

        )

    [response] => Array
        (
            [numFound] => 71199
            [start] => 0
            [docs] => Array
......
......
    [highlighting] => Array
        (
            [114360] => Array
                (
                    [Data] => Array
                        (
                            [0] => AMEki has done it better <em>russian</em>...

....
....

Two things now: 1)Can I do it quicker? 2)Can I design it better?

  • 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-06-03T02:41:51+00:00Added an answer on June 3, 2026 at 2:41 am

    this seems unneccesary

     foreach($json_O as $section1=>$items1){
        if($section1==highlighting){
            foreach($items1 as $section2=>$items2){
    

    you could simply do

            foreach($json_O['highlighting'] as $section2=>$items2){
    

    simplifying the rest is also possible, although this is untested

    $riter = new RecursiveArrayIterator($json_O['highlighting']);
    $riteriter = new RecursiveIteratorIterator($riter, RecursiveIteratorIterator::LEAVES_ONLY);
    $found = array();
    foreach ($riteriter as $key => $value) {
        $key = $riteriter->getSubIterator($riteriter->getDepth() - 2)->key();
        $found[] = compact('key', 'value');
    }
    

    personally, I would just use the nested foreach loops though. It’s very easy to understand, while my creative use of recursive iterators is not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can eregi() function can be replaced here with preg_match() function in below given
For example, my goal is to test the code given here: PHP script that
I know this should be very simple especially given all the information and example
Could someone prove to me that the advice given here (copied below) regarding removing
Given the code below, wich is a very trimmed down version of the actual
Going from the example given here... http://ericswann.org/blog/archive/2009/04/06/linq-to-sql-datacontext-provider-revisited.aspx I'm trying to use the datacontext between
I am trying to use the solution given here -> C# ADO.NET: nulls and
I compute c = a 'OR' b // bitwise OR operation here Now given
I am trying to do work with examples on Trees as given here: http://cslibrary.stanford.edu/110/BinaryTrees.html
Here's what I want to do: Given a table PeopleOutfit (id int primary key,

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.