Is it possible to connect the key genres, country, in one value
eg [genres] => value1, value2, value3
because there is a problem such that the Values from previous loop are added to the next loop
Here is printscreen http://s12.postimg.org/sv53nakej/Bez_tytu_u.png
1./////////////////////////////////////////////////////////////////////
stdClass Object
(
[title] => Marsjanin
[origTitle] => The Martian
[genres] => Array
(
[0] => Array
(
[key] => 28
[value] => akcja
)
[1] => Array
(
[key] => 33
[value] => sci-fi
)
)
[country] => Array
(
[0] => Array
(
[key] => 53
[value] => USA
)
[1] => Array
(
[key] => 53
[value] => Australia
)
)
)
2./////////////////////////////////////////////////////////////////////
stdClass Object
(
[title] => Pięćdziesiąt twarzy Greya
[origTitle] => Fifty Shades of Grey
[genres] => Array
(
[0] => Array
(
[key] => 16
[value] => melodramat
)
[1] => Array
(
[key] => 33
[value] => sci-fi
)
)
[country] => Array
(
[0] => Array
(
[key] => 53
[value] => USA
)
)
)
This is my code:
foreach( $urll as $urle){
$results = My_Parser::getMovie($urle);
foreach($results as $itemz => $valuez) {
if($itemz=='genres'){
foreach($valuez as $val) {
$genr[] = $val['value'];
}}
}
$genre = implode(', ', $genr);
echo '<tr><th>'.$genre.'</th><th>Importuj</th></tr>';
}
Values from previous loop are added to the next
ex.
A
B
C
D
Result:
A = A
A+B = B
A+B+C = C
A+B+C+D = D
Output [genres]
Array
(
[0] => akcja
[1] => sci-fi
)
Array
(
[0] => akcja / Incorect value added from first array
[1] => sci-fi / Incorect value added from first array
[2] => melodramat
[3] => sci-fi
)
Your $genr is being appended to constantly. Reset it before entering the nested loop:
foreach( $urll as $urle){
...
$genr = array(); // Add this!
foreach($results as $itemz => $valuez) {
...
}
...
$genre = implode(', ', $genr); // Now you're good.
...
}
You need to initialize the array before the foreach of foreach($valuez as $val)
foreach( $urll as $urle){
$results = My_Parser::getMovie($urle);
foreach($results as $itemz => $valuez) {
if($itemz=='genres'){
$genr=array(); //initialization
foreach($valuez as $val) {
$genr[] = $val['value'];
}
$genre = implode(', ', $genr); // use the variable -- As per as your requirement.
echo '<tr><th>'.$genre.'</th><th>Importuj</th></tr>';
}
}
}
Related
I need to create an dynamic array and can't get it right.
I need something like this:
Product Name
top
White
Black
Bottom
Red
Green
I came up with this code, which is producing the above text, so my logic must be about right.
$set = array();
$set['name'] = "Product Name";
$options = array("top", "bottom");
$values['top'] = array("White", "Black");
$values['bottom'] = array("Red", "Green");
echo "<pre>".$set['name']."</pre>";
foreach ($options as $o) {
echo "<pre>- $o</pre>";
$set['options'][]['name'] = $o;
foreach ($values[$o] as $v) {
echo "<pre>-- $v</pre>";
$set['options'][]['values']['name'] = $v;
}
}
The array that is created with the above code is:
Array
(
[name] => Product Name
[options] => Array
(
[0] => Array
(
[name] => top
)
[1] => Array
(
[values] => Array
(
[name] => White
)
)
[2] => Array
(
[values] => Array
(
[name] => Black
)
)
[3] => Array
(
[name] => bottom
)
[4] => Array
(
[values] => Array
(
[name] => Red
)
)
[5] => Array
(
[values] => Array
(
[name] => Green
)
)
)
)
THe output I want is:
Array
(
[name] => Product Name
[options] => Array
(
[0] => Array
(
[name] => top
[values] => Array
(
[0] => Array
(
[name] => White
)
[1] => Array
(
[name] => Black
)
)
)
[1] => Array
(
[name] => bottom
[values] => Array
(
[0] => Array
(
[name] => Red
)
[1] => Array
(
[name] => Green
)
)
)
)
)
What am I missing ?
You got
$set['options'][]['name'] = $o;
^
this one
and
$set['options'][]['values']['name'] = $v;
^
This one
in outer as well as inner loop which was adding new item to array (so there were indexes like 0, 1, 2 ...) so could not produce what you wanted.
You may correct your array like below:
Demo
<?php
$set = array();
$set['name'] = "Product Name";
$options = array("top", "bottom");
$values['top'] = array("White", "Black");
$values['bottom'] = array("Red", "Green");
echo "<pre>".$set['name']."</pre>";
foreach ($options as $o) {
echo "<pre>- $o</pre>";
$vals = array();
foreach ($values[$o] as $v) {
echo "<pre>-- $v</pre>";
$vals[] = array('name' => $v );
}
$set['options'][] = array('name' => $o, 'values' => $vals );
}
print_r($set);
?>
Array
(
[content_type] => Array
(
[0] => story
[1] => delhi
[2] => tez
)
[type] => Array
(
[0] => video_id
[1] => subcategory
[2] => story_id
)
[fetch_id] => Array
(
[0] => 32
[1] => 32
[2] => 2
)
[order] => Array
(
[0] => 6
[1] => 4
[2] => 5
)
[label] => Array
(
[0] => dsfs fdsf dsf sdf
[1] => dfsdfs
[2] => sdfsdfsd
)
[link] => Array
(
[0] => fsd fsdf sdf
[1] => fsdfsdfdsf
[2] => fsdfdsfds
)
[record] => Array
(
[0] => 10
[1] => 8
[2] => 12
)
)
Above is the array I have to sort this array in the basis of order field and it should shorted all the fields accordingly like below example.
$arr['order'][0] = 4;
$arr['order'][1] = 5;
$arr['order'][2] = 6;
$arr['type'][0] = 'subcategory';
$arr['type'][1] = 'story_id';
$arr['type'][2] = 'video_id';
and so on.....
You can try this -
$new = array();
// Extract and get the keys as values
$order = array_flip($array['order']);
// sort them according to keys
ksort($order);
// loop through main array
foreach($array as $key => $sub_array) {
// loop through order
foreach ($order as $o) {
// store the new value according to order
$new[$key][] = $sub_array[$o];
}
}
Demo
Some lesser solution:
asort($array['order']);
foreach ($array as $key => $subArray) {
$array[$key] = array_replace($array['order'], $subArray);
}
For reset a key sequence you may just to use array_values().
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>";
}
}
}
As a stepping stone towards building a bit of json from which to build dependent dropdown, I'd like to transform this array...
Array
(
[0] => Array
(
[id] => 4
[project_id] => 2289
[task] => Drawing
)
[1] => Array
(
[id] => 5
[project_id] => 2289
[task] => Surveying
)
[2] => Array
(
[id] => 6
[project_id] => 2289
[task] => Meeting
)
[3] => Array
(
[id] => 1
[project_id] => 2282
[task] => Folding
)
[4] => Array
(
[id] => 2
[project_id] => 2282
[task] => Printing
)
[5] => Array
(
[id] => 3
[project_id] => 2282
[task] => Cutting
)
)
..to something like this...
Array
(
[0] = Array
(
[project_id] => 2289
[task] => Array
(
[0] => Drawing
[1] => Surveying
[2] => Meeting
)
)
[1] = Array
(
[project_id] => 2282
[task] => Array
(
[0] => Folding
[1] => Printing
[2] => Cutting
)
)
)
Using...
$newArray = array();
foreach ($array as $row)
{
$newArray[$row['project_id']][] = $row['task'];
}
...I'm able to get this...
Array
(
[2289] => Array
(
[0] => Drawing
[1] => Surveying
[2] => Meeting
)
[2282] => Array
(
[0] => Folding
[1] => Printing
[2] => Cutting
)
)
... but I've forgotten how to include the associative keys in the result
You can modify your foreach simply using a index:
$newArray = array();
$index = array();
foreach ($array as $row)
{
$found = array_search( $row['project_id'], $index );
if( $found === False )
{
$found = array_push( $newArray, array( 'project_id' => $row['project_id'] ) )-1;
$index[$found] = $row['project_id'];
}
$newArray[ $found ]['task'][] = $row['task'];
}
eval.in demo
When a new project_id key is found, it is added to $index array, so — searching for it at next loop — I can retrieve the index of corresponding multi-dimensional array.
Just assign them as you would like, the project id in an index, and task continually pushing it there:
$newArray = array();
foreach ($array as $row) {
$newArray[$row['project_id']]['project_id'] = $row['project_id'];
$newArray[$row['project_id']]['task'][] = $row['task'];
}
$newArray = array_values($newArray); // reindex
// removes `$row['project_id']` on each group
Note: Just use array_values to reset the grouping key that you used in the project id grouping.
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);
}
}