I am working on online TV series website and can't figure out this one need to get values from array which is inside array.
This is structure contains $seasons variable:
Array
(
[0] => Array
(
[0] => link1-1
[1] => link1-2
[2] => link1-3
)
[1] => Array
(
[0] => link2-1
[1] => link2-2
[2] => link2-3
)
)
i need to get values like:
0
link1-1
link1-2
link1-3
1
link2-1
link2-2
link2-3
and then put them inside html video tag, but i can't figure out how to get values correctly
tried:
while ($row = $result->fetch_assoc()) {
$season_count = $row['season_count'];
$seasons = array_map("str_getcsv", explode("___", $row['en_url']));
array_shift($seasons);
for($i = 0; $i < count($seasons); $i++){
print_r($seasons[$i][*]); // i need equivalent of * to select all values of each array
}
break;
}
foreach ($seasons as $season){
foreach ($season as $link) {
echo '<video src="' . $link . '"><video>';
}
}
And next time - please formulate your task clearly.
Related
I got an array of links which I am getting from source code. I am looping through the array with a foreach loop and adding the results into a new array.
The problem is: I don't want all the results in one array. But for each link a separate array after I looped over it.
The array I am looping through:
Array
(
[0] => Array
(
[0] => http://videos.volkswagen.nl/videos/videos/
)
[1] => Array
(
[0] => http://videos.volkswagen.nl/videos/service-videos/
)
)
The foreach:
$sourceCats = array();
foreach ($matchesAll as $links) {
$strSourceAll = implode("|",$links);
$source = file_get_contents("$strSourceAll");
htmlspecialchars($source);
$sourceCats[] = $source;
}
How the array sourceCats looks now:
Array
(
[0] => (source code from first link)
[1] => (source code from second link)
)
How I want it to look like:
Array
(
[0] => Array
(
[0] => (source code from first link)
)
[1] => Array
(
[0] => (source code from second link)
)
)
I have tried a few things but nothing worked. Is the idea clear?
Any help will be much appreciated.
<?php
$finalsourceCats = array();
$counter_sourceCats = 0;
$matchesAll = array(
0 => array(
0 => "http://videos.volkswagen.nl/videos/videos/"
),
1 => array(
0 => "http://videos.volkswagen.nl/videos/service-videos/"
)
);
foreach ($matchesAll as $links) {
$sourceCats = 'sourceCats';
$sourceCats = $sourceCats . "_" . $counter_sourceCats;
$sourceCats = array();
$strSourceAll = implode("|", $links);
$source = file_get_contents("$strSourceAll");
htmlspecialchars($source);
$sourceCats[] = $source;
$finalsourceCats[] = $sourceCats;
$counter_sourceCats += 1;
}
echo "<pre>"; print_r($finalsourceCats);
In the PHP, I need to convert the below image CSV file in this array format:
This code for creating categories and subcategories. If client will add the extra categories and subcategories its need to work as per that. So I need the dynamic one.
Array Format :
Array
(
[0] => Cateory1
[Cateory1] => Array
(
[0] => SubCategory11
[1] => SubCategory12
[2] => SubCategory13
[3] => SubCategory14
)
[1] => Cateory2
[Cateory2] => Array
(
[0] => SubCategory21
[1] => SubCategory22
[2] => SubCategory23
[SubCategory23] => Array
(
[0] => SubCategory221
[1] => SubCategory222
)
[3] => SubCategory24
)
[2] => Cateory3
[Cateory3] => Array
(
[0] => SubCategory31
[1] => SubCategory32
[2] => SubCategory33
)
[3] => Cateory4
[Cateory4] => Array
(
[0] => SubCategory41
[SubCategory41] => Array
(
[0] => SubCategory411
[SubCategory411] => Array
(
[0] => SubCategory4111
[SubCategory4111] => Array
(
[0] => SubCategory41111
)
)
)
)
)
Kindly help me to achieve this one.
Thanks.
Although i agree with Pitchinnate, I had some spare time and I hate parsing that sort of csv rubbish, so I thought I give it a try. Here is a piece of code that might get you going. I did not test or optimize anything, so if this doesn't work as expected, it might still point you in the right direction.
// I assume, $imput is a twodimensional array containing the csv data
// the multi dimensional result array
$result = array();
// accumulated categories of current-1 line
$lastCat = array();
foreach($input as $line) {
// accumulated categories of current line
$cat = array();
//First we create an array in $cat that contains the full "breadcrumbs" per line e.g.
// ['category1', 'SubCategory11', 'SubCategory114', ...]
$marker = PHP_INT_MAX;
for ($i=0; $i<count($line); $i++) {
if ($marker < $i) {
$cat[$i] = '';
} else if ($line[$i] != '') {
$cat[$i] = $line[$i];
$marker = $i;
}
}
// then using these category breadcrumbs, we create the result arrays
// you could do this using recursion if you want
$tmp = $result;
for ($i=0; $i<count($cat); $i++) {
// if we haven't seen this category yet, create it
// a bit bulky, but necessary as you want the categories with numeric indices as well
if (!isset($lastCat[$i]) || $cat[$i] != $lastCat[]) {
$tmp[] = $cat[$i];
// Check if there are still subcategories left in this line...
if (!empty($cat[$i+1])) {
//... and if so, create the category named index if not already existing
if (!array_key_exists($cat[$i], $tmp)) {
$tmp[$cat[$i]] = array();
}
$tmp = $tmp[$cat[$i]];
} else {
// ... and if not, we are finished with this line
break;
}
}
}
$lastCat = $cat;
}
I've got an array that looks like this..
Array
(
[fm] => Array
(
[133.74] => Array
(
[base] => Array
(
[0] => 2015-09-29
[1] => 2015-09-30
)
)
[202.59] => Array
(
[base] => Array
(
[0] => 2015-10-01
)
)
)
[fmtax] => Array
(
[13.51] => Array
(
[0] => 2015-09-29
[1] => 2015-09-30
)
[20.46] => Array
(
[0] => 2015-10-01
)
)
)
and I need to build a SQL statement to insert the relevant values. I'm thinking it will need to be a unique query for each array item.
So I'll end up needing something like
$sql = "insert into mytable ('price', 'tax', 'date')
values ('133.74', '13.51', '2015-09-29')";
What I've got so far is this...
foreach ($sale['fm'] as $key => $value) {
$i=0;
$g=count($value['base']);
while($i < $g) {
echo($sale['fm'][$key]['base'][$i]."<br />");
$i++;
}
}
which does well for the "fm" key - but I'm getting hung up on how to grab the value from the second associated "fmtax" array key. I was thinking using something like current/next as it iterates through...but am kinda scratching my head on it.
I am assuming that fm and fmtax both have same number of elements and the order of related elements (price and tax) from fm and fmtax are same. What you can do is - store the taxes from fmtax with simple keys in this way :
$taxes = array();
$c = 0;
foreach ($sale['fmtax'] as $key => $value) {
$taxes[$c] = $key;
$c++;
}
Then you can access this new $taxes array from your own code block in this way :
$c = 0;
foreach ($sale['fm'] as $key => $value) {
//Here you have the related tax
$tax = $taxes[$c];
$i=0;
$g=count($value['base']);
while($i < $g) {
echo($sale['fm'][$key]['base'][$i]."<br />");
$i++;
}
$c++;
}
I have some php arrays from a loop, all of them bearing the same name. Now I want to merge them, but it seems not to work...
Here's my loop:
while($row = mysql_fetch_row($sql1)){
$startzeit=strtotime($row[2]);
$endzeit=strtotime($row[3]);
$startzeit_format = date("Y-m-d",$startzeit);
$endzeit_format = date("Y-m-d",$endzeit);
$datearray[] = createDateRangeArray($startzeit_format,$endzeit_format);
}
This should be the merging code:
for($i = 0; $i<count($datearray); $i++)
{
$datesarray = array_merge($datearray[$i]);
}
Anyway, the manual merge works fine:
$datesarray = array_merge( $datearray[0], $datearray[1], $datearray[2], $datearray[3]);
This one leads to the desired output. However I'd like to automatize it, as the single arrays come from a database and I won't add a $datearray[4], $datearray[5] and so on, everytime there is a new entry in the mySQL..
The result of print_r($datearray):
Array (
[0] => Array ( [0] => 2014-03-08 )
[1] => Array ( [0] => 2013-09-15 )
[2] => Array ( [0] => 2013-09-21 )
[3] => Array ( [0] => 2013-10-03
[1] => 2013-10-04
[2] => 2013-10-05
[3] => 2013-10-06 )
)
What you might be looking for is to flatten the array:
$datesarray = call_user_func_array('array_merge', $datearray);
It's identical to how you were manually merging together the array items.
See also: call_user_func_array()
You could also do this inside the loop with a simple loop:
$datesarray = array();
while ($row = mysql_fetch_row($sql1)) {
// ...
foreach (createDateRangeArray($startzeit_format,$endzeit_format) as $item) {
$datesarray[] = $item;
}
}
You are merging a single array to nothing.
$newArray = array_merge($array, $array)
Merges those two arrays but you are doing
$array = array_merge($datearray[$i]);
In affect, you are creating an array from one key of an array.
Hello I have an array that consists of two other arrays within it using the following code:
foreach($relations as $rel){
$data[$i]["relationTo"] = $rel["name"];
$data[$i]["relation"] = $rel["relation"];
$i = $i+1;
}
foreach($relations as $rel){
$children[$i]["id"] = $rel["id2"];
$children[$i]["name"] = $rel["sname"];
$children[$i]["data"] = $data;
$i = $i+1;
}
foreach($relations as $rel){
$relationArray[$i]["id"] = $rel["id"];
$relationArray[$i]["name"] = $rel["name"];
$relationArray[$i]["children"] = $children;
$i = $i+1;
}
When I print this out using:
print_r($relationArray);
It prints the following:
Array ( [2] => Array ( [id] => 4 [name] => Albaraa [children] =>
Array ( [1] => Array ( [id] => 5 [name] => Sadi [data] =>
Array ( [0] => Array ( [relationTo] => Albaraa [relation] => Father ) ) ) ) ) )
I am using json_encode and I need it to be output in json a certain way not including the indexed count of arrays in the beginning...the json output when I use:
echo json_encode($relationArray);
is like this currently:
{"2":{"id":"4","name":"Albaraa","children":
{"1":{"id":"5","name":"Sadi","data": [{"relationTo":"Albaraa","relation":"Father"}]}}}}
With the "2" and "1" in front of what the first 2 arrays are...which is not what I am trying to achieve which would be like this:
{"id":"4","name":"Albaraa","children":
{"id":"5","name":"Sadi","data": [{"relationTo":"Albaraa","relation":"Father"}]}}}}
Any help will be much appreciated!
Several solutions
1) Do not enter values by [$i], prepare new complete inner array and put it inside with array_push
2) If you still want to do this way you can extract just the values:
print_r(json_encode(array_values($array)));