YC C
YC C

Reputation: 35

Getting highest level parent from json code with php

I'm struggling on getting the get the highest id as a separate record in case there is a sub, or more subs out of a json array.

<?php 
$data = json_decode('
[{"id":122943,    
"children":[{"id":48614}]},
 {"id":10009,
"children":[{"id":74311,
    "children":[{"id":17988}]}]}]');
function parseJsonArray($jsonArray, $parentID = 0) {
$return = array();

foreach ($jsonArray as $subArray) {
    $returnSubSubArray = array();

    if (isset($subArray->children)) {
        $returnSubSubArray = 
        parseJsonArray($subArray->children, $subArray->id);     
    }
$return[] = array('id' => $subArray->id, 'parentID' => $parentID);
$return = array_merge($return, $returnSubSubArray);
}
return $return;
}

$readbleArray = parseJsonArray($data);

$i=0;
foreach($readbleArray as $row){
  $i++;
echo("update cmm_master set parent = '".$row['parentID']."', sort = '".$i."' , master = '????' where PCR = '".$row['id']."' ")."<br>";
echo ("\r\n");
}
?>

With the example above I'm trying the get the master record for each item and the outcome should be:

parent = '122943', sort = '1' , master = '122943' where PCR = '122943'
parent = '122943', sort = '2' , master = '122943' where PCR = '48614'
parent = '10009', sort = '3' , master = '10009' where PCR = '10009'
parent = '10009', sort = '4' , master = '10009' where PCR = '74311'
parent = '74311', sort = '5' , master = '10009' where PCR = '17988' 

Thanks for your help or information

Upvotes: 1

Views: 108

Answers (1)

alexander.polomodov
alexander.polomodov

Reputation: 5524

Try to use this code:

<?php
$data = json_decode('
[
    {"id":122943, "children":[
        {"id":48614}
    ]
    },
    {"id":10009, "children":[
        {"id":74311, "children":[
            {"id":17988}
        ]}
    ]}
]
');

function parseJsonArray($jsonArray, $parentID = 0, $masterID = 0)
{
    $return = array();

    foreach ($jsonArray as $subArray) {
        $returnSubSubArray = array();

        if (isset($subArray->children)) {
            $returnSubSubArray =
                parseJsonArray($subArray->children, $subArray->id, !$parentID ? $subArray->id : $masterID);
        }

        $return[] = array('id' => $subArray->id, 'parentID' => !$parentID ? $subArray->id : $parentID, 'masterID' => !$parentID ? $subArray->id : $masterID);
        $return = array_merge($return, $returnSubSubArray);
    }
    return $return;
}

$readbleArray = parseJsonArray($data);

$i = 0;
foreach ($readbleArray as $row) {
    $i++;
    echo ("update cmm_master set parent = '" . $row['parentID'] . "', sort = '" . $i . "' , master = '".$row['masterID']."' where PCR = '" . $row['id'] . "' ");
    echo("\r\n");
}
?>

It's output looks like:

update cmm_master set parent = '122943', sort = '1' , master = '122943' where PCR = '122943' 
update cmm_master set parent = '122943', sort = '2' , master = '122943' where PCR = '48614' 
update cmm_master set parent = '10009', sort = '3' , master = '10009' where PCR = '10009' 
update cmm_master set parent = '10009', sort = '4' , master = '10009' where PCR = '74311' 
update cmm_master set parent = '74311', sort = '5' , master = '10009' where PCR = '17988' 

Upvotes: 1

Related Questions