Multi Dimension array to convert in string in php - php

Array ( [11] => Array ( [0] => A [1] => Attempt ) [ 12] => Array ( [0] => 0 [1] => None ) [ 13] => Array ( [0] => 0 [1] => None ) [ 14] => Array ( [0] => 0 [1] => None ) [ 15] => Array ( [0] => 0 [1] => None ) [ 16] => Array ( [0] => 0 [1] => None ) )
This is my array but i want in below STRING format:
11=>A=>Attempt,12=>0=>None,13=>0=>None,14=>0=>None,15=>0=>None,16=>0=>None
1. $keys=array_keys($total_answer)
2. for($i=0;$i<count($keys);$i++)
3. {
4. for($j=0;$j<count($total_answer[$keys[$i]]);$j++)
5. {
6. echo $total_answer[$keys[$i]][$j]
7. //Here I am getting confuse to make string
Thank You In Advance:)

One way to do it,
<?php
$array = array ( "11" => array ( "0" => "A" ,"1" => "Attempt" ) ,"12" => array ( "0" => 0, "1" => "None" ) ,"13" => array ( "0" => 0, "1" => "None" ) ,"14" => array ( "0" => 0 ,"1" => "None" ), "15" => array ( "0" => 0, "1" => "None" ), "16" => array ( "0" => 0, "1" => "None" ) );
foreach($array as $key=>$value){
$expected[] = $key.'=>'.$value[0].'=>'.$value[1];
}
echo implode(',',$expected);
?>
WORKING DEMO: https://3v4l.org/TDb0A

One foreach is enough
$out = [];
foreach ($total_answer as $k=>$v)
{
array_unshift($v, $k);
$out[] = implode('=>', $v);
}
echo implode(',', $out);

Related

Return a sub array with two specific key/value pairs

Consider the following array returned from an API:
$arr = Array
(
[A] =>
[C] =>
[D] =>
[EE] => Array
(
[0] => Array
(
[B] =>
[C] => 2.06
[O] =>
[P] => 0
[T] => 1
)
[1] => Array
(
[B] =>
[C] => 2.56
[O] =>
[P] => 0
[T] => 2
)
[2] => Array
(
[B] =>
[C] => 4.94
[O] =>
[P] => 0
[T] => 3
)
[3] => Array
(
[B] =>
[C] => 1.42
[O] =>
[P] => 1
[T] => 9
)
[4] => Array
(
[B] =>
[C] => 2.83
[O] =>
[P] => 1
[T] => 10
)
[5] => Array
(
[B] =>
[C] => 2.13
[O] =>
[P] => 1.5
[T] => 9
)
[6] => Array
(
[B] =>
[C] => 1.7
[O] =>
[P] => 1.5
[T] => 10
)
)
)
I want to get the C value from the sub array where P is 1.5 and T is 9. Obviously if I knew this would always be in sub array with index 5 I could just do this:
$arr['EE'][5]['C'];
But 5 will not always be the index of that particular array. So I'm looking for something more along the lines of:
$arr['EE'][...P is 1.5 and T is 9...]['C'];
This is within a loop that processes thousands of these arrays, so performance is definitely a consideration. In other words, I don't want to do a bunch of nested loops to find that value - I'm looking for a built-in PHP function for this (or a combination thereof) if it exists.
You can also use array_reduce
$arr = array
(
"A" => "",
"C" => "",
"D" => "",
"EE" => array
(
"0" => array
(
"B" => "",
"C" => 2.06,
"O" => "",
"P" => 0,
"T" => 1,
),
"1" => array
(
"B" => "",
"C" => 2.56,
"O" => "",
"P" => 0,
"T" => 2,
),
"2" => array
(
"B" => "",
"C" => 4.94,
"O" => "",
"P" => 0,
"T" => 3,
),
"3" => array
(
"B" => "",
"C" => 1.42,
"O" => "",
"P" => 1,
"T" => 9,
),
"4" => array
(
"B" => "",
"C" => 2.83,
"O" => "",
"P" => 1,
"T" => 10,
),
"5" => array
(
"B" => "",
"C" => 2.13,
"O" => "",
"P" => 1.5,
"T" => 9,
),
"6" => array
(
"B" => "",
"C" => 1.7,
"O" => "",
"P" => 1.5,
"T" => 10,
),
)
);
$p = 1.5;
$t = 9;
$result = array_reduce( $arr["EE"], function( $c, $v ) use ($p,$t) {
if ( $v["P"] == $p && $v["T"] == $t ) $c = $v["C"];
return $c;
}, "" );
echo $result;
This will result to:
2.13
Other option: You can use array_filter
$p = 1.5;
$t = 9;
$result = array_filter( $arr["EE"], function( $v ) use ($p,$t) {
return $v["P"] == $p && $v["T"] == $t;
} );
$result = array_values( $result );
echo "<pre>";
print_r( $result );
echo "</pre>";
This will result to
Array
(
[0] => Array
(
[B] =>
[C] => 2.13
[O] =>
[P] => 1.5
[T] => 9
)
)
So accessing C is $result[0]["C"]
Doc: array_filter
Or for performance, you can use foreach loop
$p = 1.5;
$t = 9;
$result = "";
foreach( $arr["EE"] as $v ) {
if ( $v["P"] == $p && $v["T"] == $t ) $result = $v["C"];
}
echo $result;

I want to merge two dimensional arrays

Let me describe you the scenario
I have got two arrays one with second column as 0
and another array with second column with some value,
first array
Array
(
[0] => Array
(
[time_frame] => 2017-05-19
[email_data] => 0
)
[1] => Array
(
[time_frame] => 2017-05-20
[email_data] => 0
)
[2] => Array
(
[time_frame] => 2017-05-21
[email_data] => 0
)
[3] => Array
(
[time_frame] => 2017-05-22
[email_data] => 0
)
[4] => Array
(
[time_frame] => 2017-05-23
[email_data] => 0
)
[5] => Array
(
[time_frame] => 2017-05-24
[email_data] => 0
)
[6] => Array
(
[time_frame] => 2017-05-25
[email_data] => 0
)
second array
Array
(
[0] => Array
(
[time_frame] => 2017-05-20
[email_data] => 1
)
[1] => Array
(
[time_frame] => 2017-05-21
[email_data] => 2
)
[2] => Array
(
[time_frame] => 2017-05-24
[email_data] => 8
)
)
Now what I want to achieve is the data below:
[
{
"time_frame": "2017-05-19",
"email_data": "0"
},
{
"time_frame": "2017-05-20",
"email_data": "1"
},
{
"time_frame": "2017-05-21",
"email_data": "2"
},
{
"time_frame": "2017-05-22",
"email_data": "0"
},
{
"time_frame": "2017-05-23",
"email_data": "0"
},
{
"time_frame": "2017-05-24",
"email_data": "8"
},
{
"time_frame": "2017-05-25",
"email_data": "0"
}
]
Thanks in advance.
Check out this piece of code...
$data1 = array(
array('time_frame'=>'2017-05-19', 'email_data'=>0),
array('time_frame'=>'2017-05-20', 'email_data'=>0),
array('time_frame'=>'2017-05-21', 'email_data'=>0),
array('time_frame'=>'2017-05-22', 'email_data'=>0),
array('time_frame'=>'2017-05-23', 'email_data'=>0),
array('time_frame'=>'2017-05-24', 'email_data'=>0),
array('time_frame'=>'2017-05-25', 'email_data'=>0)
);
$data2 = array(
array('time_frame'=>'2017-05-20', 'email_data'=>1),
array('time_frame'=>'2017-05-21', 'email_data'=>2),
array('time_frame'=>'2017-05-24', 'email_data'=>8)
);
foreach ($data2 as $key2 => $value2)
{
foreach ($data1 as $key1 => $value1)
{
if($value2['time_frame'] == $value1['time_frame']) {
$data1[$key1] = $value2;
}
}
}
echo '<pre>';
print_r($data1);
echo '</pre>';
Why you do it using loop, php provide array_merge() function use it
example is here..
first array..
$first = array(
0 => Array
(
"time_frame" => "2017-05-20",
"email_data" => "1"
),
1 => Array
(
"time_frame" => "2017-05-21",
"email_data" => "2"
),
2 => Array
(
"time_frame" => "2017-05-24",
"email_data" => "8"
)
);
second array...
$second = array(
0 => Array
(
"time_frame" => "2017-05-19",
"email_data" => 0
),
1 => Array
(
"time_frame" => "2017-05-20",
"email_data" => 0
),
2 => Array
(
"time_frame" => "2017-05-21"
,"email_data" => 0
),
3 => Array
(
"time_frame" => "2017-05-22"
,"email_data" => 0
),
4 => Array
(
"time_frame" => "2017-05-23"
,"email_data" => 0
),
5 => Array
(
"time_frame" => "2017-05-24"
,"email_data" => 0
),
6 => Array
(
"time_frame" => "2017-05-25"
,"email_data" => 0
)
);
now your solutions is here..
$combined_array = array_merge($first,$second);
and it give you required result. Simple.
for printing result...
foreach($combined_array as $key){
print_r($key);
echo "<br> - - - - - - - - - - - - - - -- - - - - - - <br>";
}

PHP search multidimensional array with two keys for value of 3rd key?

I would think this is simple, searching a multidimensional array with two keys to bring back the value of the third. I'm more confused then when I started and unable to get it to work.
Array (
[0] => Array ( [data] => Array ( [name] => Definite Position [Company_ID] => 4 [code] => DEF ) )
[1] => Array ( [data] => Array ( [name] => First Option [Company_ID] => 7 [code] => TNT ) )
[2] => Array ( [data] => Array ( [name] => Second Option [Company_ID] => 4 [code] => SEC ) )
[3] => Array ( [data] => Array ( [name] => Definite Out [Company_ID] => 6 [code] => DBO ) )
I would like to bring back the value of [name] when I have [Company_ID] of 4 and [code] of 'SEC'
Any help would be appreciated. Thanks
It's just a simple loop accessing the data index and then the indexes below that:
$id = 4;
$code = 'SEC';
foreach($array as $values) {
if($values['data']['Company_ID'] == $id && $values['data']['code'] == $code) {
$result = $values['data']['name'];
break; // we found it no need to loop more
}
}
This might help:
<?php
$array = array(
array(
"data" => array(
"name" => "Definite Position",
"Company_ID" => 4,
"code" => "DEF"
)
),
array(
"data" => array(
"name" => "First Option",
"Company_ID" => 7,
"code" => "TNT"
)
),
array(
"data" => array(
"name" => "Second Option",
"Company_ID" => 4,
"code" => "SEC"
)
),
array(
"data" => array(
"name" => "Definite Out",
"Company_ID" => 6,
"code" => "DBO"
)
)
);
/**
* Searches the array for matching criteria
*
* #param array $search Array of criteria to search for (eg. array("Company_ID" => 4, "code" => "SEC"))
* #param array $array The array to search
*
* #return array The elements of the array that matched the criteria
*/
function search($search, $array)
{
$retVal = array();
foreach($array as $k => $v)
{
$found = true;
foreach($search as $sKey => $sVal)
{
if ($v["data"][$sKey] != $sVal)
{
$found = false;
break;
}
}
if ($found)
$retVal[]= $v;
}
return $retVal;
}
$results = search(
array(
"Company_ID" => 4,
"code" => "SEC"
),
$array
);
var_dump($results);
Result:
array(1) {
[0]=>
array(1) {
["data"]=>
array(3) {
["name"]=>
string(13) "Second Option"
["Company_ID"]=>
int(4)
["code"]=>
string(3) "SEC"
}
}
}

Deep Associative Array to adjacent mysql table

Been fighting the whole night. Giving up. I have an adjacent table in mysql:
id, parentid,name,design,path,sort
The depth is maximum four and using mysql query, I print out the results to UL list successfully. From there, items are added, sorted and edited as well as removed. Then when button is clicked, I send the result back to php. The data been sent is JSON and it does get recieved.
json_decode() gives the following sample:
Array ( [0] => Array ( [cls] => [path] => # [id] => 1 [name] =>BLOCKA ) [1] => Array ( [cls] => [path] => # [id] => 2 [name] => BLOCKB [children] => Array ( [0] => Array ( [cls] => [path] => # [id] => 3 [name] => CLASSB1 [children] => Array ( [0] => Array ( [cls] => [path] => # [id] => 7 [name] => CLASSB12 ) ) ) [1] => Array ( [cls] => [path] => # [id] => 4 [name] => CLASSSB13 [children] => Array ( [0] => Array ( [cls] => [path] => # [id] => 5 [name] => CLASSB4 ) [1] => Array ( [cls] => [path] => # [id] => 6 [name] => CLASSB5 ) ) ) ) ) )
Graphically:
BLOCKA
BLOCKB
CLASSB1
CLASSB3
...
I am using jquery.nested
Now my problem is looping through the array, getting id of the parent then add child.
The closest I came with is
function dissect($blocks) {
if (!is_array($blocks)) {
echo $blocks;
return;
}
foreach($blocks as $block) {
dissect($block);
}
}
It does process each element but not in the way I want. Sorry for my broken english...any help would be appreciated.
First iterate through blocks getting the parent, if children exists in parent block, loop through the children.
There might be other better way to implement this, you can try below code:
$blocks = array (
"0" => array (
"cls" => "",
"path" => array(
"id" => 1,
"name" =>"BLOCKA",
)
),
"1" => array (
"cls" => "",
"path" => array(
"id" => 2,
"name" => "BLOCKB" ,
"children" => array (
"0" => array (
"cls" => "",
"path" => array(
"id" => 3,
"name" => "CLASSB1" ,
"children" => array (
"0" => array (
"cls" => "",
"path" => array(
"id" => 7,
"name" => "CLASSB12" ,
),
),
),
),
),
"1" => array (
"cls" => "",
"path" => array(
"id" => 4,
"name" => "CLASSSB13" ,
"children" => array (
"0" => array (
"cls" => "",
"path" => array(
"id" => 5,
"name" => "CLASSB4" ,
),
),
"1" => array (
"cls" => "",
"path" => array(
"id" => 6,
"name" => "CLASSB5",
),
),
),
),
),
),
),
),
) ;
echo "<pre>";
/*loop through blocks*/
foreach ($blocks as $key => $block) {
echo $block['path']['name'].'<br/>'; /* echo parent*/
if (isset($block['path']['children'])) {
loopChildren($block['path']['children']); /* children loop*/
}
}
/*Loop through childrens*/
function loopChildren($childrens, $prefix = '-')
{
foreach ($childrens as $key => $child) {
getChild($child, $prefix);
}
}
/*Get the child and loop sub-children if exist*/
function getChild($child, $prefix='-')
{
echo $prefix. $child['path']['name'].'<br/>'; /*echo child*/
if (isset($child['path']['children'])) {
$prefix .= '-';
loopChildren($child['path']['children'], $prefix); /* sub-children loop*/
}
}
echo "</pre>";
The output:
BLOCKA
BLOCKB
-CLASSB1
--CLASSB12
-CLASSSB13
--CLASSB4
--CLASSB5
thanks for your tip. It guided me to an answer that worked for me. I ended up using two functions. The error was "index out of bound"...here is what I did now...with calls to the mysql table:
$response = json_decode($_POST['s'], true); // decoding received JSON to array
if(is_array($response)){
//start saving now
$order=1;
foreach ($response as $key => $block) {
//0 is for blocks: no parent id needed
$parentid=$this->Blocks_model->insertblock($block['name'],$block['cls'],0,$block['path'],$order);
if (isset($block['children'])) {
$this->childblocks($parentid,$block['children']);
}
$order++;
}
}
private function childblocks($parentid,$e){
$order=1;
foreach ($e as $key => $block) {
$parentid=$this->Blocks_model->insertblock($block['name'],$block['cls'],0,$block['path'],$order);
if (isset($block['children'])) {
$this->childblocks($parentid,$block['children']);
}
$order++;
}
}

update php multidimensional array

I want to update array based on pid and size s if product id and size both are matched whithin same array elements then it update the quantity by adding both the quantity and merge them in one index.Any help should be appreciated.
my code is:_
<?php
$details = array ( "0" => array ( "pid" => "402",
"q" => "1",
"s" => "0"
),
"1" => array ( "pid" => "403",
"q" => "2",
"s" => "0"
),
"2" => array ( "pid" => "402",
"q" => "3",
"s" => "0"
),
"3" => array ( "pid" => "403",
"q" => "1",
"s" => "0"
),
"4" => array ( "pid" => "405",
"q" => "1",
"s" => "0"
),
);
?>
Output Should look like
output :
0-pid 402 q 4 s 0
1-pid 403 q 3 s 0
4-pid 405 q 1 s 0
foreach($details as $p)
{
$output[$p["pid"]][$p["s"]]["q"]+=$p["q"];
}
Output
Array
(
[402] => Array
(
[0] => Array
(
[q] => 4
)
)
[403] => Array
(
[0] => Array
(
[q] => 3
)
)
[405] => Array
(
[0] => Array
(
[q] => 1
)
)
)
You can then display the array in any format you like

Categories