How to define variable inside array without key? This doesnt working and I dont know how...
$array = array("list" => array());
$list = $array["list"][] = array("sub_list" = array());
$list["sub_list"][] = "text1";
$list["sub_list"][] = "text2";
$list["sub_list"][] = "text2";
$list2 = $array["list"][] = array("sub_list" = array());
$list2["sub_list"][] = "text1";
$list2["sub_list"][] = "text2";
$list2["sub_list"][] = "text3";
Needed result:
$array = array(
"list" => array(
array(
"sub_list" = array("text1", "text2", "text3")
),
array(
"sub_list" = array("text1", "text2", "text3")
)
)
);
It's not used in loop or for/foreach!
$array = [
'list' => []
];
$list = [];
$list[] = 'text1';
$list[] = 'text2';
$list[] = 'text3';
$array['list'][]['sub_list'] = $list;
$array['list'][]['sub_list'] = $list;
$list = [];
$list[] = 'text4';
$list[] = 'text5';
$list[] = 'text6';
$array['list'][]['sub_list'] = $list;
And you will have :
$array = array(
"list" => array(
array(
"sub_list" => array("text1", "text2", "text3")
),
array(
"sub_list" => array("text1", "text2", "text3")
),
array(
"sub_list" => array("text4", "text5", "text6")
)
)
);
$array["list"][] = array("sub_list" => array());
$list= [];
$list[] = array("text1", "text2", "text3");
$list[] = array("text1", "text2", "text3");
$array["list"][]["sub_list"] = $list;
althought it's array inside array and this array is also in array
i need $array["list"][] as variable, when is called $array["list"][]["sub_list"] = $list; is created new one array, i need add "sub_list" to first array
Related
I am looping though one initial array, and specifying values i want. Then i am storing the values i need in an array. Then i am combining those values into a new array, with keys and values. The new array is only storing the last entry of all the data that has been passed to it.
$exampleArray = array();
for ($i = 0; $i < 100; $i++){
$exampleArray[] = array(
$A1 = $Anotherarray[$i][25],
$A2 = $Anotherarray[$i][26],
$A3 = $Anotherarray[$i][24],
$A4 = $Anotherarray[$i][27],
$A5 = $Anotherarray[$i][28]
);
$secondExample = array();
foreach( $A1 as $i => $val )
{
$secondExample[] = array(
"Field1" => $val,
"Field2" => ucfirst($A2[$i]),
"Field3" => ucfirst($A3[$i]),
"Field4" => ucfirst($A4[$i]),
"Field5" => ucfirst($A5[$i])
);
}
You are declaring $secondExample as a new array on every iteration. Do it like this:
$exampleArray = array();
$secondExample = array();
for ($i = 0; $i < 100; $i++){
$exampleArray[] = array(
$A1 = $Anotherarray[$i][25],
$A2 = $Anotherarray[$i][26],
$A3 = $Anotherarray[$i][24],
$A4 = $Anotherarray[$i][27],
$A5 = $Anotherarray[$i][28]
);
$secondExample[$i] = array();
foreach( $A1 as $j => $val) {
$secondExample[$i][] = array(
"Field1" => $val,
"Field2" => ucfirst($A2[$j]),
"Field3" => ucfirst($A3[$j]),
"Field4" => ucfirst($A4[$j]),
"Field5" => ucfirst($A5[$j])
);
}
As because you are overwriting every time the loop goes.Use array_push
$secondExample = array();
foreach( $A1 as $i => $val )
{
$varArray = array(
"Field1" => $val,
"Field2" => ucfirst($A2[$i]),
"Field3" => ucfirst($A3[$i]),
"Field4" => ucfirst($A4[$i]),
"Field5" => ucfirst($A5[$i])
);
array_push($secondExample, $varArray);
}
I am trying to merge two arrays, but getting NULL. Below is my code
$a = 1;
foreach($codes as $values) {
$id = $values['id'];
$post_data = array (
"id" => $id,
"name" => $this->input->post('Name'),
"from_date" => $this->input->post('FromDate'),
"to_date" => $this->input->post('ToDate')
);
$this->data['output' . $a++] = $this->my_modal->simple_post($post_data);
}
$this->data['output'] = array_merge($this->data['output1'], $this->data['output2']);
var_dump($this->data['output']);
Any suggestions will be appreciated. Thanks..
You have to delete the first parameter (NULL) of array_merge();
And what is $this->input->$id? Don't you mean $id?
And in this environment, it's better to use array_push();:
$a = 1;
$this->data['output'] = array();
foreach($codes as $values)
{
$id = $values['id'];
$post_data = array (
"id" => $id,
"name" => $this->input->post('Name'),
"from_date" => $this->input->post('FromDate'),
"to_date" => $this->input->post('ToDate')
);
$new_data = $this->my_modal->simple_post($post_data);
array_push($this->data['output'], $new_data);
}
var_dump($this->data['output']);
$a = 1;
$this->data['output'] = array();
foreach($codes as $values){
$id = $values['id'];
$post_data = array (
"id" => $id,
"name" => $this->input->post('Name'),
"from_date" => $this->input->post('FromDate'),
"to_date" => $this->input->post('ToDate')
);
$data['output2']= $this->my_modal->simple_post($post_data);
if(count($this->data['output1']) > 1) {
$this->data['all'] = array_merge($this->data['output1'],$data['output2']);
}else {
$this->data['all'] = $data['output1'];
}
}
print_r($this->data['all']);
Your code is perfectly right, the only problem is that you start the counter with $a = 1, and do $a++ which will result in 2. So the output1 does not exist. However if you write (notice the subtle change):
$a = 1;
foreach($codes as $values) {
$id = $values['id'];
$post_data = array (
"id" => $id,
"name" => $this->input->post('Name'),
"from_date" => $this->input->post('FromDate'),
"to_date" => $this->input->post('ToDate')
);
$this->data['output' . $a] = $this->my_modal->simple_post($post_data); // $a = 1 now
$a++; // $a becomes 2 here
}
$this->data['output'] = array_merge($this->data['output1'], $this->data['output2']);
var_dump($this->data['output']);
I have a form that is posting 4 arrays that I need to combine and eventually compose to email. The four arrays:
$quantityArray = $this->input->post('qty');
$dimensionArray = $this->input->post('dimension');
$thicknessArray = $this->input->post('thickness');
$descriptionArray = $this->input->post('description');
will all have the same array length and each index will be related. How do I combine the 4 arrays such as
[0]
'qty' => '1',
'dimenesion => '2x2',
'thickness' => '2in',
'description' => 'this is the description'
[1]
'qty' => '1',
'dimenesion => '2x2',
'thickness' => '2in',
'description' => 'this is the description'
I have tried array_combined, array_merged and can't get the results that I am looking for. Thanks for the help on this.
If you have same length for those arrays here is example code:
$resultArray = array();
foreach($quantityArray as $index => $qty) {
$resultArray[$index]['qty'] = $qty;
$resultArray[$index]['dimenesion'] = $dimensionArray[$index];
$resultArray[$index]['thickness'] = $thicknessArray[$index];
$resultArray[$index]['description'] = $descriptionArray [$index];
}
print_r($resultArray);
This also may work:
<?php
//...
$quantityArray = $this->input->post('qty');
$dimensionArray = $this->input->post('dimension');
$thicknessArray = $this->input->post('thickness');
$descriptionArray = $this->input->post('description');
//
// combine them:
//
$combined = array();
$n = count($quantityArray);
for($i = 0; $i < $n; $i++)
{
$combined[] = array(
'qty' => $quantityArray[$i],
'dimenesion' => $dimensionArray[$i],
'thickness' => $thicknessArray[$i],
'description' => $descriptionArray[$i]
);
}
//
echo "<pre>";
print_r($combined);
echo "</pre>";
?>
If we assume that we have same length for those arrays here is my codes:
$quantityArray = array(1, 1, 5, 3);
$dimensionArray = array("2x2", "3x3", "4x4", "2x2");
$thicknessArray = array("2in", "3in", "4in", "2in");
$descriptionArray = array("this is the description 1", "this is the description 2 ", "this is the description3 ", "this is the description4" );
$myCombinArray = array();
foreach ( $quantityArray as $idx => $val ) {
$subArray = array (
'qty' => $quantityArray [$idx],
'dimenesion' => $dimensionArray [$idx],
'thickness' => $thicknessArray [$idx],
'description' => $descriptionArray [$idx]
);
array_push ( $myCombinArray, $subArray );
}
print_r($myCombinArray);
How about an easy way if there are always those 4 arrays:
$quantityArray = $this->input->post('qty');
$dimensionArray = $this->input->post('dimension');
$thicknessArray = $this->input->post('thickness');
$descriptionArray = $this->input->post('description');
$combinedArray = [$quantityArray, $dimensionArray, $thicknessArray, $descriptionArray];
# old syntax:
# $combinedArray = array($quantityArray, $dimensionArray, $thicknessArray, $descriptionArray);
I want to get the list for the food from the database into array order by category so that I can split each entry into the categories.
Like this..
$menu = array(
'Appetizers' => array(
'Chicken Tenders' => '$2.99',
'Twisted Chips' => '$1.99'
),
'Seafood' => array(
'Bayou Tilapia' => '$4',
'Grill Atlantic Salmon' => '$3.99'
),
'Steaks & Combos' => array(
'Cowboy Grande Sirloin' => '$7.99'
)
)
Here is what I did.
$db->Query("SELECT menuTitle,menuCategory,menuPrice FROM menu");
$menu = array();
while ($row = $db->Row()) {
$a = array($row->menuCategory => array($row->menuTitle=>$row->menuPrice));
array_push($menu,$a);
}
It doesn't seem to work. Would you please advise how to achieve this?
replace:
$a = array($row->menuCategory => array($row->menuTitle=>$row->menuPrice));
array_push($menu,$a);
with:
$menu[$row->menuCategory][$row->menuTitle]=$row->menuPrice;
The code in the while block is not exactly seeming right, as you always redefine the $a array while iterating.
while ($row = $db->Row()) {
$a[ $row->menuCategory ][ $row->menuTitle] = $row->menuPrice;
array_push($menu,$a);
}
This way, you will only append new keys to the array.
$db->Query("SELECT menuTitle,menuCategory,menuPrice FROM menu");
$menu = array(); $a = array();
while ($row = $db->Row()) {
$a[$row->menuCategory ][$row->menuTitle] = $row->menuPrice;
}
array_push($menu,$a);
echo "<pre>";print_r($menu);echo "</pre>";
I'm trying to get this working:
I have an array that gets "deeper" every loop. I need to add a new array to the deepest "children" key there is.
while($row = mysql_fetch_assoc($res)) {
array_push($json["children"],
array(
"id" => "$x",
"name" => "Start",
"children" => array()
)
);
}
So, in a loop it would be:
array_push($json["children"] ...
array_push($json["children"][0]["children"] ...
array_push($json["children"][0]["children"][0]["children"] ...
... and so on. Any idea on how to get the key-selector dynamic like this?
$selector = "[children][0][children][0][children]";
array_push($json$selector);
$json = array();
$x = $json['children'];
while($row = mysql_fetch_assoc($res)) {
array_push($x,
array(
"id" => "$x",
"name" => "Start",
"children" => array()
)
);
$x = $x[0]['children'];
}
print_r( $json );
Hmmm - maybe better to assign by reference:
$children =& $json["children"];
while($row = mysql_fetch_assoc($res)) {
array_push($children,
array(
"id" => "$x",
"name" => "Start",
"children" => array()
)
);
$children =& $children[0]['children'];
}
$json = array();
$rows = range('a', 'c');
foreach (array_reverse($rows) as $x) {
$json = array('id' => $x, 'name' => 'start', 'children' => array($json));
}
print_r($json);
If you want to read an array via a string path, split the string in indices, and then you can do something like this to get the value
function f($arr, $indices) {
foreach ($indices as $key) {
if (!isset($arr[$key])) {
return null;
}
$arr = $arr[$key];
}
return $arr;
}