How would I go about splitting this array to access and loop through each of these videos...
array(1) {
[0]=> array(2)
{
[0]=> array(3)
{
["title"]=> string(27) "A test title for this video"
["video_item"]=> string(70) "http://dev.test/wp-content/uploads/2014/01/1.Introduction3.mp4"
["video_image"]=> string(78) "http://dev.test/wp-content/uploads/2014/01/1.Introduction3_thumb23.jpg"
}
[1]=> array(3)
{
["title"]=> string(13) "asdf fads fad"
["video_item"]=> string(67) "http://dev.test/wp-content/uploads/2014/01/Spring-Mower.mp4"
["video_image"]=> string(75) "http://dev.test/wp-content/uploads/2014/01/Spring-Mower1_thumb6.jpg"
}
}
}
This is part of the code I am using but obviously not working
// this gets the array
$videos = get_post_meta( get_the_ID(), 'video_items', false );
$vid = array();
$img = array();
foreach( $videos as $video ) {
$vid[] = $video['video_item'];
$img[] = $video['video_image'];
}
You have an array within an array, so you need to access the first element before you start iterating through each array inside that
So just add this line after you get the array $videos = fullArray[0];
// this gets the array as you did in your original code block
$fullArray = get_post_meta( get_the_ID(), 'video_items', false );
//But then you actually needed to add the below line. This gets the first
//element of the array which happens to be an array and actually contains the array you
//originally wanted to iterate through
$videos = fullArray[0];
$vid = array();
$img = array();
foreach( $videos as $video ) {
$vid[] = $video['video_item'];
$img[] = $video['video_image'];
}
echo "video urls " . $vid . "\n";
echo "image urls " . $img;
Maybe you can use array_chunk to have it's pieces.
http://us2.php.net/manual/en/function.array-chunk.php
As said in the documentation:
<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>
Will print
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => c
[1] => d
)
[2] => Array
(
[0] => e
)
)
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[2] => c
[3] => d
)
[2] => Array
(
[4] => e
)
)
try this
foreach($array as $key => value)
{
if(is_array($value))
{
foreach($value as $k => $v)ev
{
foreach($v as $k1 => $v1)
{
echo $k1 .'=>'.$v1.PHP_EOL;
}
}
}
}
even better will be using a RecursiveIterator here
Related
I have this problem. I have a multidimensional json array (don't ask - legacy code). I convert it to PHP array. I need to extract all KEYS that have EQUAL VALUE (in my case "666"). I need each key to be assign to a SEPARATE variable. In my code I succeeded to fetch the keys but I can assign them only to another array or all at once to a single variable. Please HELP!!!
Love V
Here is the code:
<?php
//user input
$in = "666";
$outputZ = '[
{"record_id_001":"1"},
{"record_id_002":"13"},
{"record_id_003":"666"},
{"record_id_004":"72661781"},
{"record_id_005":"8762"},
{"record_id_006":"666"},
{"record_id_007":"8762"},
{"record_id_008":"666"},
{"record_id_009":"8762"},
{"record_id_010":"8762"},
{"record_id_011":"666"}
]';
//convert json to php array
//someArray = json_decode($someJSON, true);
$decoZ = json_decode($outputZ, true);
// TESTING (move to comment latter)
//print_r ($decoZ);
//loop through each array an check for user input
//way 1: assign to new array
foreach($decoZ as $array => $number)
foreach($number as $key => $value)
if (in_array("$in", $number)) {
$var = $key;
$getarray = "'" . $var . "'";
$recnumber = array($getarray);
print_r ($recnumber);
}
//way 2: assign to variable
foreach($decoZ as $array => $number)
foreach($number as $key => $value)
if (in_array("$in", $number)) {
$var = $key;
echo "$var" . " ";
}
?>
You're overwritting your array on each iterations $recnumber = array($getarray);
I would rather follow this logic :
<?php
//user input
$in = "666";
$outputZ = '[
{"record_id_001":"1"},
{"record_id_002":"13"},
{"record_id_003":"666"},
{"record_id_004":"72661781"},
{"record_id_005":"8762"},
{"record_id_006":"666"},
{"record_id_007":"8762"},
{"record_id_008":"666"},
{"record_id_009":"8762"},
{"record_id_010":"8762"},
{"record_id_011":"666"}
]';
$decoZ = json_decode($outputZ, true);
// create empty array
$recnumber = [];
foreach($decoZ as $record)
{
foreach($record as $key => $value)
{
// simply compare input with current value
if ($value === $in)
{
// add the key to the previously created array
$recnumber[] = $key;
}
}
}
var_dump($recnumber);
This outputs :
array(4) {
[0]=>
string(13) "record_id_003"
[1]=>
string(13) "record_id_006"
[2]=>
string(13) "record_id_008"
[3]=>
string(13) "record_id_011"
}
You can simply remap the records.
At the end you can grab them like
$codes666 = $ids['666'];
$outputZ = '[
{"record_id_001":"1"},
{"record_id_002":"13"},
{"record_id_003":"666"},
{"record_id_004":"72661781"},
{"record_id_005":"8762"},
{"record_id_006":"666"},
{"record_id_007":"8762"},
{"record_id_008":"666"},
{"record_id_009":"8762"},
{"record_id_010":"8762"},
{"record_id_011":"666"}
]';
$records = json_decode($outputZ);
$ids = [];
foreach($records as $record) {
foreach($record as $key => $value) {
$ids[(string)$value][] = $key;
}
}
print_r($ids);
Gives
Array
(
[1] => Array
(
[0] => record_id_001
)
[13] => Array
(
[0] => record_id_002
)
[666] => Array
(
[0] => record_id_003
[1] => record_id_006
[2] => record_id_008
[3] => record_id_011
)
[72661781] => Array
(
[0] => record_id_004
)
[8762] => Array
(
[0] => record_id_005
[1] => record_id_007
[2] => record_id_009
[3] => record_id_010
)
)
I'm get data string lat and long google maps polygon. I want to convert this value to array .
$value = "(-6.2811957386588855, 106.70141951079609),(-6.281142416506361, 106.70432702536823),(-6.2781776962328815, 106.70438066954853),(-6.2781776962328815, 106.70136586661579)";
I want the result like this Array :
$polygon = array(
array(-6.2811957386588855, 106.70141951079609),
array(-6.281142416506361, 106.70432702536823),
array(-6.2781776962328815, 106.70438066954853),
array(-6.2781776962328815, 106.70136586661579),
);
You can convert the string to valid JSON by converting parentheses to square brackets and adding a [] layer around the outside, and then json_decode it:
$value = '(-6.2811957386588855, 106.70141951079609),(-6.281142416506361, 106.70432702536823),(-6.2781776962328815, 106.70438066954853),(-6.2781776962328815, 106.70136586661579)';
$polygon = json_decode('[' . str_replace(array('(', ')'), array('[', ']'), $value) . ']', true);
print_r($polygon);
Output:
Array
(
[0] => Array
(
[0] => -6.2811957386589
[1] => 106.7014195108
)
[1] => Array
(
[0] => -6.2811424165064
[1] => 106.70432702537
)
[2] => Array
(
[0] => -6.2781776962329
[1] => 106.70438066955
)
[3] => Array
(
[0] => -6.2781776962329
[1] => 106.70136586662
)
)
Demo on 3v4l.org
Using preg_match_all() and array_walk() you can parse the coordinates as an array
$value = '(-6.2811957386588855, 106.70141951079609),(-6.281142416506361, 106.70432702536823),(-6.2781776962328815, 106.70438066954853),(-6.2781776962328815, 106.70136586661579)';
preg_match_all('/\(([0-9\-\s,.]+)\)/', $value, $matches);
array_walk($matches[1], function(&$val) { $val = explode(',', $val); });
$coordinates = $matches[1];
print_r($coordinates);
Using preg_match_all() get all the coordinates as array of string
Using array_walk() make an iteration over the coordinated array and explode by the delimiter of comma (,)
Working demo.
Regex demo.
You can use explode() function like this
$string = '(-6.2811957386588855, 106.70141951079609),(-6.281142416506361, 106.70432702536823),(-6.2781776962328815, 106.70438066954853),(-6.2781776962328815, 106.70136586661579)';
foreach(explode('),(',trim($string,'()')) as $single_array)
{
$sub_array= array();
foreach(explode(',',$single_array) as $sbs_array)
{
$sub_array[] = $sbs_array;
}
$result[] = $sub_array;
}
print_r ($result);
Output :
Array
(
[0] => Array
(
[0] => -6.2811957386588855
[1] => 106.70141951079609
)
[1] => Array
(
[0] => -6.281142416506361
[1] => 106.70432702536823
)
[2] => Array
(
[0] => -6.2781776962328815
[1] => 106.70438066954853
)
[3] => Array
(
[0] => -6.2781776962328815
[1] => 106.70136586661579
)
)
Demo : https://3v4l.org/6HEAG
You can use explode with trim and array_map
$r = explode('),(',trim($value,'()'));
$c = array_map(function($v){return explode(',',$v);}, $r);
print_r($c);
Working example : https://3v4l.org/fFAS0
I suggest using preg_match_all with array_map
$value = '(-6.2811957386588855, 106.70141951079609),(-6.281142416506361, 106.70432702536823),(-6.2781776962328815, 106.70438066954853),(-6.2781776962328815, 106.70136586661579)';
preg_match_all('#\(([\-\d\.]+),\s+([\-\d\.]+)\)#', $value, $matches);
$geo = array_map(function ($a, $b) {
return [(float)$a, (float)$b];
}, $matches[1], $matches[2]);
Output:
array(4) {
[0]=>
array(2) {
[0]=> float(-6.2811957386589)
[1]=> float(106.7014195108)
}
[1]=>
array(2) {
[0]=> float(-6.2811424165064)
[1]=> float(106.70432702537)
}
[2]=>
array(2) {
[0]=> float(-6.2781776962329)
[1]=> float(106.70438066955)
}
[3]=>
array(2) {
[0]=> float(-6.2781776962329)
[1]=> float(106.70136586662)
}
}
Regex
I have an array like this:
$arr = array(
'home.js' => new File(),
'view/index.html' => new File(),
'src/index.js' => new File(),
'src/libs/jquery.js' => new File()
);
Now I want to convert in a structure like this:
Array
(
[0] => Array
(
[text] => home.js
)
[1] => Array
(
[text] => view
[children] => Array
(
[0] => Array
(
[text] => index.html
)
)
)
[2] => Array
(
[text] => src
[children] => Array
(
[0] => Array
(
[text] => index.js
)
[1] => Array
(
[text] => libs
[children] => Array
(
[0] => Array
(
[text] => jquery.js
)
)
)
)
)
)
I tried for hours, with help of StackOverfow answers but I couldn't come up with a solution as all other questions have a different setup.
Edit:
What I got so far with the help of SO is (can't remember the exact answer though):
$out = array();
foreach($arr as $path => $file) {
$parts = explode('/', trim($path, '/'));
applyChain($out, $parts, $file);
}
function applyChain(&$arr, $parts, $value)
{
if (!is_array($parts)) {
return;
}
if (count($parts) == 0) {
$arr = $value;
} else {
array_shift($parts);
applyChain($arr[], $parts, $value);
}
}
print_r($out);
I don't know how exactly it works, especially the part applyChain($arr[] ...). It kinda works with the depth, but not with the file names. I get following output:
Array
(
[0] => File Object
(
)
[1] => Array
(
[0] => File Object
(
)
)
[2] => Array
(
[0] => File Object
(
)
)
[3] => Array
(
[0] => Array
(
[0] => File Object
(
)
)
)
)
There would be a solution in a few lines using explode() and eval(). But eval() is not considered clean, so lets try recursion:
<?php
class File {
}
$arr = array(
'home.js' => new File(),
'view/index.html' => new File(),
'src/index.js' => new File(),
'src/libs/jquery.js' => new File()
);
function sub($path) {
$rv = array();
$parts = explode('/', $path, 2); // strip off one level
$rv['text'] = $parts[0]; // put it into 'text' element
if (count($parts)>1) // is there anything left?
$rv['children'] = sub($parts[1]); // do the same for the rest of the path
return $rv;
}
$new = array();
foreach (array_keys($arr) as $file) {
$new[] = sub($file);
}
var_dump($new);
?>
But, as Peter commented, this creates seperate substructures even if the pathes have some part in common (like src/libs/jquery.js and src/libs/melon.js).
With the use of ugly eval() (which can be replaced later) I got the following code:
<?php
class File {
}
$arr = array(
'home.js' => new File(),
'view/index.html' => new File(),
'src/index.js' => new File(),
'src/libs/jquery.js' => new File(),
'src/libs/melon.js' => new File(),
);
// conversion
function sub($element) {
$rv = array();
foreach (array_keys($element) as $sub) {
$part['text'] = $sub;
if (is_array($element[$sub])) {
$part['children'] = sub($element[$sub]);
}
$rv[] = $part;
}
return $rv;
}
// create array with path file/folder names as keys
$new = array();
foreach (array_keys($arr) as $row) {
$def = '$new["'.preg_replace('&/&', '"]["', $row).'"] = 1;';
eval($def);
}
// run
$new2 = sub($new);
var_dump($new2);
?>
This outputs
array(3) {
[0]=>
array(1) {
["text"]=>
string(7) "home.js"
}
[1]=>
array(2) {
["text"]=>
string(4) "view"
["children"]=>
array(1) {
[0]=>
array(1) {
["text"]=>
string(10) "index.html"
}
}
}
[2]=>
array(2) {
["text"]=>
string(3) "src"
["children"]=>
array(2) {
[0]=>
array(1) {
["text"]=>
string(8) "index.js"
}
[1]=>
array(2) {
["text"]=>
string(4) "libs"
["children"]=>
array(2) {
[0]=>
array(1) {
["text"]=>
string(9) "jquery.js"
}
[1]=>
array(1) {
["text"]=>
string(8) "melon.js"
}
}
}
}
}
}
I have an array that looks like this:
array(3) {
[0]=>
array(2) {
[0]=>
string(10) "2012-11-14"
[1]=>
string(5) "3238"
}
[1]=>
array(2) {
[0]=>
string(10) "2012-11-13"
[1]=>
string(5) "3231"
}
[2]=>
array(2) {
[0]=>
string(10) "2012-11-13"
[1]=>
string(5) "3231"
}
I would like to write a foreach loop that would turn this array into:
array(2) {
[0]=>
array(1) {
"2012-11-14" => "3238"
}
[1]=>
array(1) {
"2012-11-13" => "3231"
}
So, basically, I would like to use the array element formatted as Y-M-D date as key to the second element in the array.
Given the following array...
$array = array(
0 => array(0 => "2012-11-14", 1 => "3238"),
1 => array(0 => "2012-11-13", 1 => "3231"),
2 => array(0 => "2012-11-13", 1 => "3231"),
);
putting it into a new array like this:
$new_array = array();
foreach ($array as $key => $item)
{
$new_array[$key][$item[0]] = $item[1];
}
print_r($new_array);
produces this output:
Array
(
[0] => Array
(
[2012-11-14] => 3238
)
[1] => Array
(
[2012-11-13] => 3231
)
[2] => Array
(
[2012-11-13] => 3231
)
)
My answer doesn't get rid of the duplicates, but the added dimension as specified in the original question means that duplicate dates as keys aren't an issue.
<?php
$data = array(
array("2012-11-14", "3238"),
array("2012-11-13", "3231"),
array("2012-11-13", "3231") // warning! when there are two record with same date, the second's count will be display
);
$result = array();
foreach ($data as $value) {
$result[$value[0]] = $value[1];
}
echo '<pre>';
print_r($result);
<?php
$newArray = array();
for($i=0;$i<count($arrayVariable);$i++)
{
$newArray[$arrayVariable[$i][0]] = $arrayVariable[$i][1];
}
echo '<pre>';print_r($newArray);echo '</pre>';
?>
Didn't test it but something like this should work in concept. Of course change arrayVariable to your variable.. but that aside.
You can use this code to get what you want:
$dates = array(
array("2012-11-01", "3238"),
array("2012-11-03", "4321")
);
print_r($dates);
$result = array();
foreach($dates as $value) {
$result[][$value[0]] = $value[1];
}
print_r($result);
The output will look like the requested form:
Array
(
[0] => Array
(
[2012-11-01] => 3238
)
[1] => Array
(
[2012-11-03] => 4321
)
)
Codepad demo: http://codepad.org/XAmUEdYh
However, I would personally prefer Aykut's solution. You would of course have a problem when you've got two records with the same date, but the overall array layout is a bit nicer ;).
Here is what I came up with:
<?php
$original = array(
array(
"2012-11-14",
"3238"
),
array(
"2012-11-13",
"3231"
),
array(
"2012-11-13",
"3231"
)
);
$newArray = array();
foreach($original as $subArray){
$newArray[] = array($subArray[0] => $subArray[1]);
}
var_dump($newArray);
I have some Problems reducing a multidimensional array into a normal one.
I have an input array like this:
Array
(
[0] => Array (
[0] => 17
[1] => 99
)
[1] => Array (
[0] => 17
[1] => 121
)
[2] => Array (
[0] => 99
[1] => 77
)
[3] => Array (
[0] => 45
[1] => 51
)
[4] => Array (
[0] => 45
[1] => 131
)
So I have a multidimensional array with some overlaps in the values (eg 17,99 and 17,121)
Now I want to have an output like this:
Array
(
[0] => Array (
[0] => 17
[1] => 99
[2] => 121
[3] => 77
)
[2] => Array (
[0] => 45
[1] => 51
[3] => 131
)
I want to save, which articles are the same in my database this way. The output array shpuld still be a multidimesional array, but every number on the second level should be unique in the array.
I'm trying to solve this for more than a week now, but I dont get it to work. I know it should be easy...but anyway - I dont get it :D
This is what i got so far:
$parity_sorted = array();
foreach($arr as $key => $a){
if(count($parity_sorted) > 0){
foreach($parity_sorted as $key2 => $arr_new){
if(in_array($a[0], $arr_new) || in_array($a[1], $arr_new)){
if(!in_array($a[0], $arr_new)){array_push($parity_sorted[$key2], $a[0]);}
if(!in_array($a[1], $arr_new)){array_push($parity_sorted[$key2], $a[1]);}
} else {
array_push($parity_sorted, array($a[0],$a[1]));
}
}
} else {
array_push($parity_sorted, array($a[0],$a[1]));
}
}
Did you maybe already solve problem like this or is there a much easier way? Maybe I just think too complicated (It's not my first try, but this code was the last try)
Any help would be appreciated. Thanks a lot
Here is my revised code given your comment and a DEMO of it working as expected. ( http://codepad.org/CiukXctS )
<?php
$tmp = array();
foreach($array as $value)
{
// just for claraty, let's set the variables
$val1 = $value[0];
$val2 = $value[1];
$found = false;
foreach($tmp as &$v)
{
// check all existing tmp for one that matches
if(in_array($val1, $v) OR in_array($val2, $v))
{
// this one found a match, add and stop
$v[] = $val1;
$v[] = $val2;
// set the flag
$found = true;
break;
}
}
unset($v);
// check if this set was found
if( ! $found)
{
// this variable is new, set both
$tmp[] = array(
$val1,
$val2,
);
}
}
// go trough it all again to ensure uniqueness
$array = array();
foreach($tmp as $value)
{
$array[] = array_unique($value); // this will eliminate the duplicates from $val2
}
ORIGIN ANSWER
The question is badly asked, but I'll attempt to answer what I believe the question is.
You want to gather all the pairs of arrays that have the same first value in the pair correct?
$tmp = array();
for($array as $value)
{
// just for claraty, let's set the variables
$val1 = $value[0];
$val2 = $value[1];
if(isset($tmp[$val1])) // we already found it
{
$tmp[$val1][] = $val2; // only set the second one
}
else
{
// this variable is new, set both
$tmp[$val1] = array(
$val1,
$val2,
);
}
}
// go trough it all again to change the index to being 0-1-2-3-4....
$array = array();
foreach($tmp as $value)
{
$array[] = array_unique($value); // this will eliminate the duplicates from $val2
}
Here is solution for common task.
$data = array(array(17,99), array(17,121), array(99,77), array(45,51), array(45,131));
$result = array();
foreach ($data as $innner_array) {
$intersect_array = array();
foreach ($result as $key => $result_inner_array) {
$intersect_array = array_intersect($innner_array, $result_inner_array);
}
if (empty($intersect_array)) {
$result[] = $innner_array;
} else {
$result[$key] = array_unique(array_merge($innner_array, $result_inner_array));
}
}
var_dump($result);
Try:
$arr = array(array(17,99),
array(17,121),
array(99,77),
array(45, 51),
array(45, 131)
);
foreach($arr as $v)
foreach($v as $m)
$new_arr[] = $m;
$array = array_chunk(array_unique($new_arr), 4);
var_dump($array);
Demo
It uses array_unique and array_chunk.
Output:
array(2) { [0]=>array(4) { [0]=> int(17) [1]=>int(99)
[2]=>int(121) [3]=> int(77) }
[1]=> array(3) { [0]=> int(45) [1]=>int(51)
[2]=>int(131) }
}
I think I get your problem. Let me have a crack at it.
$firstElems = array();
$secondElems = array();
foreach ( $arr as $v ) {
$firstElems[ $v[0] ] = array( $v[0] );
}
foreach ( $arr as $v ) {
$secondElems[ $v[1] ] = $v[0];
}
foreach ( $arr as $v ) {
if ( isset( $secondElems[ $v[0] ] ) ) {
array_push( $firstElems[ $secondElems[ $v[0] ] ], $v[1] );
}
else {
array_push( $firstElems[ $v[0] ], $v[1] );
}
}
foreach ( $firstElems as $k => $v ) {
if ( isset( $secondElems[ $k ] ) ) {
unset( $firstElems[ $k ] );
}
}
Output:
Array
(
[17] => Array
(
[0] => 17
[1] => 99
[2] => 121
[3] => 77
)
[45] => Array
(
[0] => 45
[1] => 51
[2] => 131
)
)
(Code examples: http://codepad.org/rJNNq5Vd)
I truly believe I understand you and if this is the case here is what you're looking for:
function arrangeArray($array) {
$newArray = array(array_shift($array));
for ($x = 0; $x < count($newArray); $x++) {
if (!is_array($newArray[$x])) {
unset($newArray[$x]);
return $newArray;
}
for ($i = 0; $i < count($newArray[$x]); $i++) {
foreach ($array as $key => $inArray) {
if (in_array($newArray[$x][$i], $inArray)) {
$newArray[$x] = array_unique(array_merge($newArray[$x], $inArray));
unset($array[$key]);
}
}
}
$newArray[] = array_shift($array);
}
}
Which will return:
array(2) {
[0]=>
array(4) {
[0]=>
int(17)
[1]=>
int(99)
[2]=>
int(121)
[4]=>
int(77)
}
[1]=>
array(3) {
[0]=>
int(45)
[1]=>
int(51)
[3]=>
int(131)
}
}
For:
var_dump(arrangeArray(array(
array(17,99),
array(17,121),
array(99,77),
array(45, 51),
array(45, 131),
)));
And:
array(1) {
[0]=>
array(6) {
[0]=>
int(17)
[1]=>
int(99)
[2]=>
int(121)
[3]=>
int(45)
[4]=>
int(77)
[6]=>
int(51)
}
}
For:
var_dump(arrangeArray(array(
array(17,99),
array(17,121),
array(99,77),
array(45, 51),
array(45, 17),
)));