Hello guys i have this multiple array but i don't realy know how to access the values can i get all values of the company for example i will build an table with this values as shown bellow: is it possible to
build an nested while loop?
<?php
while (($company_name = current($aCompanys)) !== FALSE ){
echo key($aCompanys).'<br />';
next($aCompanys);
}
?>
[CompanyName1+] => Array (
[Zen] => Array (
[article] => Array (
[0] => Array (
[0] => Kalender
[1] => 9.99
[2] => 2017
)
[1] => Array (
[0] => Notizbuch DINA A4
[1] => 24.99
[2] => 2017
)
)
)
[PlenkS] => Array (
[article] => Array (
[0] => Array (
[0] => Kugelschreiber
[1] => 19.99
[2] => 2017
)
)
)
)
[CompanyName2] => Array (
[asdasd] => Array (
[article] => Array (
[0] => Array (
[0] => Kugelschreiber
[1] => 19.99
[2] => 2017
)
)
)
)
Since you want to print the company names as CompanyName1 : Zen , CompanyName2 : Plenks
Please find below complete code for your array, it works fine and gives the desired output.
<?php
$main_array = array("CompanyName"=>array("Zen"=>array("Article"=>array(array('kalendar','9.99','2017'),array('Notizbuch DINA A4','24.99','2017'))),"Plensk"=>array("Article"=>array(array('Kugelschreiber','9.99','2017')))),"CompanyName2"=>array("Zen2"=>array(1,2,3),"Plensk2"=>array(1,2,3)));
var_dump($main_array);
echo "<br /><br />";
foreach ($main_array as $i => $values){
foreach ($values as $key => $t){
echo $i . ":" . $key . "<br />";
}
}
?>
you can access them for example:
$aCompanys[CompanyName1+][Zen][article][0][2];
to get this value: "2017"
Related
I have this array in a foreach-loop with the 'as' $train
Now i want to read out the 'mat'-node.
I've tried this:
<?php
echo "<!-- ";
foreach ($train['mat'] as $mat) {
echo "Mat:" . $mat . "";
}
echo " -->";
?>
But it gives a empty foreach result in mijn HTML-source, between the comments.
Array
(
[status] => 0
[via] => Utrecht C., Houten, Geldermalsen
[bestemming] => Tiel
[vleugels] => Array
(
[0] => Array
(
[stopstations] => Array
(
[0] => Array
(
[naam] => Vleuten
[code] => VTN
)
[1] => Array
(
[naam] => Utrecht Terwijde
[code] => UTT
)
[2] => Array
(
[naam] => Utrecht Leidsche Rijn
[code] => UTLR
)
)
[bestemming] => Tiel
[mat] => Array
(
[0] => Array
(
[0] => SLT-4
[1] => Tiel
[2] => 2422
)
[1] => Array
(
[0] => SLT-4
[1] => Tiel
[2] => 2464
)
)
)
)
[vervoerder] => NS
[spoor] => 6
)
Hope this help.
But I think you need read more about PHP Array and Array index
echo "<!-- ";
foreach($train['vleugels'][0]['mat'] as $mat) {
echo "Mat:".$mat[2]."<br>";
}
echo " -->";
// <!-- 2422 2464 -->
I am generating dynamic textbox for save and add more functionality and i have to store that data in database i got the array but i dont know how to put that array in to loop so i can get my data.
Array looks like this based on this prepare loop so i can access every element of array:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a1
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a2
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
)
Thanks
try this
$array = //your array
foreach($array as $value){
foreach($value as $value2){
foreach($value2 as $value3){
foreach($value3 as $key3 => $value3){
//$key3 is rem_type, phase_nameā¦
//$value3 is required values
}
}
}
}
To get to the data you can use something like:
foreach ($array AS $row) {
$prem_type = $row[0][0]['prem_type'];
$phase_name = $row[0][1]['phase_name'];
$counter = $row[0][2]['counter'];
$block1 = $row[0][3]['block'];
$block2 = $row[0][4]['block'];
}
An alternative is to restructure the array into something more tidy:
$result = array();
foreach ($array AS $rowId => $row) {
$result[$rowId] = array(
'prem_type' => $row[0][0]['prem_type'],
'phase_name' => $row[0][1]['phase_name'],
'counter' => $row[0][2]['counter'],
'block1' => $row[0][3]['block'],
'block2' => $row[0][4]['block']
);
}
This results in:
Array
(
[0] => Array
(
[prem_type] => 1,
[phase_name] => a1,
[counter] => 2,
[block1] => A,
[block2] => B
)
...
)
Here is the answer:
for($i=0;$i<count($data1);$i++){
for($j=0;$j<count($data1[$i]);$j++){
$prem_type =$data1[$i][$j][0]['prem_type'];
$prem_name= $data1[$i][$j][1]['phase_name'];
$no_of_phase= $data1[$i][$j][2]['counter'];
echo $prem_type." ".$prem_name." ".$no_of_phase."<br>";
for($k=3;$k<count($data1[$i][$j]);$k++){
echo $data1[$i][$j][$k]['unitname']."<br>";
}
}
}
This is the array I have:
Array
(
[0] => name:string:255
[1] => weight:integer
[2] => description:string:255
[3] => age:integer
)
I want it to look like this
NewArray
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
[1] => Array
(
[0] => weight
[1] => integer
[2] => Array
(
[0] => description
[1] => string
[2] => 255
[3] => Array
(
[0] => age
[1] => integer
)
Or better yet, I would prefer this result. Making two separate arrays based off the number of groups.
NewArray1
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
[1] => Array
(
[0] => description
[1] => string
[2] => 255
)
NewArray2
(
[0] => Array
(
[0] => weight
[1] => integer
[1] => Array
(
[0] => age
[1] => integer
)
I have tried exploding and implode and foreaching but I'm not quite getting the result I want.
Use array_reduce() to build a new array while iterating over the old, splitting it up by type:
$array = [
'name:string:255',
'weight:integer',
'description:string:255',
'age:integer',
];
$result = array_reduce($array, function(&$result, $item) {
$parts = explode(':', $item, 3);
$result[$parts[1]][] = $parts;
return $result;
}, []);
Try this:
<?php
$array = array("name:string:255", "weight:integer", "description:string:255", "age:integer");
function map_func($value) {
return explode(':', $value);
}
$newArray = array_map(map_func, $array);
echo "Output 1:\n";
print_r($newArray);
$sorted = array();
foreach($newArray as $el)
$sorted[$el[1]][] = $el;
echo "Output 2:\n";
print_r($sorted);
Output:
Output 1:
Array
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
)
[1] => Array
(
[0] => weight
[1] => integer
)
[2] => Array
(
[0] => description
[1] => string
[2] => 255
)
[3] => Array
(
[0] => age
[1] => integer
)
)
Output 2:
Array
(
[string] => Array
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
)
[1] => Array
(
[0] => description
[1] => string
[2] => 255
)
)
[integer] => Array
(
[0] => Array
(
[0] => weight
[1] => integer
)
[1] => Array
(
[0] => age
[1] => integer
)
)
)
I doubt the following is the best solution but it does return what you wanted.
$array = array(
"0" => "name:string:255",
"1" => "weight:integer",
"2" => "description:string:255",
"3" => "age:integer"
);
foreach ($array as $key => $val) {
foreach (explode(':', $val) as $part) {
$new_array[$key][] = $part;
}
}
print_r($new_array);
above returns the following.
Array
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
)
[1] => Array
(
[0] => weight
[1] => integer
)
[2] => Array
(
[0] => description
[1] => string
[2] => 255
)
[3] => Array
(
[0] => age
[1] => integer
)
)
So you have a 1-dimentional array of strings ($arr01) . strings are separated by :
and you need to have a 2-dimentional array ($arr02) where the second dimension is an array of strings composed by splitting the initial set of strings based on their separator character :
$arr01 = array("name:string:255", "weight:integer", "description:string:255", "age:integer");
$arr02 = array(array());
for($i=0; $i<sizeof($arr01); $i++) {
$arr02[$i] = explode(":",$arr01[$i]);
}
display the two arrays....
echo "array 01: <br>";
for($i=0; $i<sizeof($arr01); $i++) {
echo "[".$i."] ".$arr01[$i]."<br>";
}
echo "<br><br>";
echo "array 02: <br>";
for($i=0; $i<sizeof($arr01); $i++) {
echo "[".$i."] ==> <br>";
for($j=0; $j<sizeof($arr02[$i]); $j++) {
echo " [".$j."] ".$arr02[$i][$j]." <br>";
}
}
echo "<br><br>";
I am trying to get the values from this multidimentional array.its pattern is as follows by using print_r($link_array);
The array is actually from the google rss api.
Array ( [0] => Array ( [title] => World Cup draw -- group-by-group analysis [link] => http://www.dawn.com/news/1061096/world-cup-draw-group-by-group-analysis [author] => [publishedDate] => Sat, 07 Dec 2013 02:31:51 -0800 [contentSnippet] => GROUP A: Brazil, Croatia, Mexico, Cameroon - Having hit form in spectacular style earlier this year by winning the ... [content] =>asdfasdf
[categories] => Array ( [0] => Sport/Football ) ) [1] => Array ( [mediaGroups] => Array ( [0] => Array ( [contents] => Array ( [0] => Array ( [url] => http://i.dawn.com/primary/2013/12/52a2f7a584e2a.jpg [type] => image/jpeg [medium] => image [title] => Brazil's head coach Luiz Felipe Scolari. -Photo by AP [thumbnails] => Array ( [0] => Array ( [url] => http://i.dawn.com/primary/2013/12/52a2f7a584e2a.jpg ) ) ) ) ) )
I have tried following three ways
echo $link_array[0];
echo $link_array[title];
foreach($link_array as $key=>$value){
echo $key;
}
Pleas help.
Your guess is mostly right, however you're printing its keys, just try acessing value['title'] and you'll get the title:
foreach($link_array as $key=>$value){
echo $value['title'];
}
You need to first assign the array to a variable.
$link_array = Array ( [0] => Array ( [title] => World Cup draw -- group-by-group analysis [link] => http://www.dawn.com/news/1061096/world-cup-draw-group-by-group-analysis [author] => [publishedDate] => Sat, 07 Dec 2013 02:31:51 -0800 [contentSnippet] => GROUP A: Brazil, Croatia, Mexico, Cameroon - Having hit form in spectacular style earlier this year by winning the ... [content] =>asdfasdf
[categories] => Array ( [0] => Sport/Football ) ) [1] => Array ( [mediaGroups] => Array ( [0] => Array ( [contents] => Array ( [0] => Array ( [url] => http://i.dawn.com/primary/2013/12/52a2f7a584e2a.jpg [type] => image/jpeg [medium] => image [title] => Brazil's head coach Luiz Felipe Scolari. -Photo by AP [thumbnails] => Array ( [0] => Array ( [url] => http://i.dawn.com/primary/2013/12/52a2f7a584e2a.jpg ) ) ) ) ) )
then you can do excatly as you have done
foreach ($link_array as $key=>$value):
echo $key . " - " . $value;
endforeach;
but this will not display your internal arrays within the array. For that you would need to do a for each inside a foreach if is_array($link_array[$key]))
foreach ($link_array as $key=>$value):
$new = $link_array[$key];
if (is_array($new)):
foreach ($new as $key1=>$value1):
echo $key1 . " - " . $value1;
endforeach;
else:
echo $key . " - " . $value;
endif;
endforeach;
You can go as many levels deep as you want and change the echo.
You could also call a function within a function:
// call it
loopfor($link_array);
function loopfor ($DATA) {
foreach ($DATA as $key=>$value):
if (is_array($DATA[$key])):
loopfor($DATA[$key]);
else:
echo $key . " - " . $value;
endif;
endforeach;
}
Hell once again, I am wondering how do you go about adding data to a new array index when inside a foreach loop?
the code I have atm is,
// Connect to the database to gather all data pertaiing to the link in question
$assoResult = mysql_query("SELECT * FROM associate_users");
while ($assoRow = mysql_fetch_field($assoResult)) {
$resultArray[] = $assoRow->name;
}
// Connect to the database to gather all data pertaiing to the link in question
$assoResult2 = mysql_query("SELECT * FROM associate_users WHERE id='$getID'");
while ($assoRow2 = mysql_fetch_object($assoResult2)) {
foreach ($resultArray as $row) {
$array = array(array( 1 => $assoRow2->$row, 2 => $row, ),);
echo "<br />"; print_r($array);
}
}
Below is the outputted data that comes from the "echo "br />"; print_r($array);" line.
=================================================================
Array ( [0] => Array ( [1] => 1 [2] => id ) )
Array ( [0] => Array ( [1] => Bob[2] => contactName ) )
Array ( [0] => Array ( [1] => Bob's Tyres [2] => company ) )
Array ( [0] => Array ( [1] => XXXXXXXXXXXXXX [2] => address1 ) )
Array ( [0] => Array ( [1] => XXXXXXXXXXXXXX [2] => address2 ) )
Array ( [0] => Array ( [1] => XXXXXXXXX [2] => address3 ) )
Array ( [0] => Array ( [1] => XXXXXX [2] => postcode ) )
As you can see the array is being created a new over and over, what I need is for the above data to increment the 1st dimension index key on every loop, so it looks like...
=================================================================
Array ( [0] => Array ( [1] => 1 [2] => id ) )
Array ( [1] => Array ( [1] => Bob[2] => contactName ) )
Array ( [2] => Array ( [1] => Bob's Tyres [2] => company ) )
Array ( [3] => Array ( [1] => XXXXXXXXXXXXXX [2] => address1 ) )
Array ( [4] => Array ( [1] => XXXXXXXXXXXXXX [2] => address2 ) )
Array ( [5] => Array ( [1] => XXXXXXXXX [2] => address3 ) )
Array ( [6] => Array ( [1] => XXXXXX [2] => postcode ) )
Thank you in advance I am out of all options in getting this to work and desperate.
Dan.
Change the values assigning part of your code to
$count=0;
foreach ($resultArray as $row) {
$array[$count][1] = $assoRow2->$row
$array[$count][2]=$row;
$count++;
echo "<br />"; print_r($array);
}
This code gets you the output you asked for without inefficiently using two queries:
// Connect to the database to gather all data pertaining to the link in question
$result = mysql_query("SELECT * FROM associate_users WHERE id=" . (int)$getID);
$resultArray = array();
$resultCount = 0;
$row = mysql_fetch_assoc($result);
$count = 0;
foreach ($row as $key => $value) {
$temp = array();
$temp[$count] = array(1 => $value, 2 => $key);
$count++;
echo "<br />"; print_r($temp);
}
Why you want it like this, I have no idea.
//extra code.declaring array
$array = array();
while ($assoRow2 = mysql_fetch_object($assoResult2)) {
foreach ($resultArray as $row) {
// 1st parameter is the array name, here array name is array .give gd name according to use
array_push($array,array( 1 => $assoRow2->$row, 2 => $row, ));
echo "<br />"; print_r($array);
}
}