I am trying to pull dynamic element in single array but result is not showing properly
Ex:
$array =array();
$element="'abc1','abc2'";
$array=array('abc',$element);
//I want result like that:
array[
[0]=>abc,
[1]=>abc1,
[2]=>ab
]
If you need to parse a string of elements to an array you can use one of the csv functions. You may then merge your arrays.
$array = array('abc');
$string_elements = "'abc1','abc2'";
$array_elements = str_getcsv($string_elements, ',', "'");
$array = array_merge($array, $array_elements);
var_export($array);
Output:
array (
0 => 'abc',
1 => 'abc1',
2 => 'abc2',
)
Alternatively to add each array element to the end of another array you can push them like so using a splat:
array_push($array, ...$array_elements);
According to my understanding, $element is storing string not an array so even if you try to get output it will display in following format
Array
(
[0] => abc
[1] => 'abc1','abc2'
)
Instead of this you can store array in $element variable and use array_merge() function to get required output e.g.
$element = array('abc1','abc2');
$array = array('abc');
$result = array_merge($array,$element);
// Output
Array
(
[0] => abc
[1] => abc1
[2] => abc2
)
Related
I have a set of values as a string, like so:
MyCustomProductID1
MyCustomProductID2
Then I proceed to create an array out of these values with explode( "\n", $myProductIdString)
Now I have additional data (strings) that I want to combine with the value from my first array. I want that simple array into a multidimensional one:
Array
(
[0] => Array
(
[id] => MyCustomProductID1
[url] => http://example.com/MyCustomProductID1.jpg
)
[1] => Array
(
[id] => MyCustomProductID2
[url] => http://example.com/MyCustomProductID2.jpg
)
)
How do I get that first array into a multidimensional one and push data along with it?
Instead of direct assigning values to array use loop-
<?php
$str = "MyCustomProductID1
MyCustomProductID2";
$arr = explode("\n", $str);
$result = [];
$url_array = ["http://example.com/MyCustomProductID1.jpg", "http://example.com/MyCustomProductID2.jpg"];
for($i=0;$i<count($arr);$i++){
$result[$i]['id'] = $arr[$i];
$result[$i]['url'] = $url_array[$i];
}
print_r($result);
?>
I'm working with multi-dimensional arrays in PHP. I want to store two string values in a two dimensional array. I have tried the following code:
$arr[][]=['string1']['string2'];
I have also tried:
$arr[][]="string1","string2";
Due to the comma I have a syntax error.
How can I fix this?
$arr = [];
$arr[] = ['string1'];
$arr[] = ['string2'];
// or simply
// $arr = [['string1'], ['string2']];
print_r($arr);
// output:
Array ( [0] => Array ( [0] => string1 ) [1] => Array ( [0] => string2 ) )
If you want 'string1' and 'string2' to be the key and subkey in an array then you use:
$arr = [];
$arr['string1']['string2'] = 'somevalue';
I have an array
print_r($myarray);
Array ( [0] => text one ) Array ( [0] => text two ) Array ( [0] => text three ) ...
When i use slice
print_r(array_slice($myarray,1,2));
Array ( ) Array ( ) Array ( ) Array ( ) ......
I get an empty array, how can i slice this array in half?
Thanks
You actually have an array that contains arrays which probably might be what is causing the issue for you. Though I do not see how you get the result you posted... It mighty be that you actually apply the slice function to each element of the output array. Then certainly you will get an empty array for each iteration. As to be expected, since each element you iterate over contains only a single element. So slicing from position 1 will result in an empty array each time...
Consider this simple example using a plain array:
<?php
$input = ["one", "two", "three", "four", "five", "six"];
$output = array_slice($input, 1, 2);
print_r($output);
The output is:
Array
(
[0] => two
[1] => three
)
So php's array_slice() function works just as expected...
Same with an array of arrays as you suggest in your post:
<?php
$input = [["one"], ["two"], ["three"], ["four"], ["five"], ["six"]];
$output = array_slice($input, 1, 2);
print_r($output);
The output of that is, as expected:
Array
(
[0] => Array
(
[0] => two
)
[1] => Array
(
[0] => three
)
)
Also considering your second comment below, that you have some words in a single string (which mighty be what you describe) I get a meaningful result myself:
<?php
$input = explode(' ', "one two three four five six");
$output = array_slice($input, 1, 2);
print_r($output);
The output, as expected, is:
Array
(
[0] => two
[1] => three
)
I'm building an array piece by piece following a specific pattern.
For example, I have this string <val0=0, val1=<val2=2, val3=<val4=4>>, val5=5> and I need to translate it to an associative array. So every time I find < I have to create a new array and store the following elements until the next >.
The string above should result in something like this:
Array
(
[val0] => 0
[val1] => Array
(
[val2] => 2
[val3] => Array
(
[val4] => 4
)
)
[val5] => 5
)
Everything is working fine for non-multidimensional arrays using str_split to break the string in pieces and iterating over them in a for loop but I'm having difficulties to find a workaround every time there is a nesting array in the string.
What I need is a way to have a pointer to the last created array inside the main array.
Is there a way to store an array pointer reference in a variable so I could do this:
print_r($MULTIARRAY['val1']['val3']);
// prints: array()
$pointer = pointer($MULTIARRAY['val1']['val3']);
$pointer[] = 'AAA';
$pointer[] = 'BBB';
print_r($MULTIARRAY['val1']['val3']);
// prints: array(
// [0] => AAA
// [1] => BBB
//)
Here you go, it's called reference
$a[1][22] = array();
$pointer = &$a[1][22];
$pointer[] = 3;
$pointer[] = 4;
print_r($a);
I have two arrays, one is generated by using explode() on a comma separated string and the other is generated from result_array() in Codeigniter.
The results when doing print_r are:
From explode():
Array
(
[0] => keyword
[1] => test
)
From database:
Array
(
[0] => Array
(
[name] => keyword
)
[1] => Array
(
[name] => test
)
)
I need them to match up so I can use array_diff(), what's the best way to get them to match? Is there something other than result_array() in CI to get a compatible array?
You could create a new array like this:
foreach($fromDatabase as $x)
{
$arr[] = $x['name'];
}
Now, you will have two one dim arrays and you can run array_dif.
$new_array = array();
foreach ($array1 as $line) {
$new_array[] = array('name' => $line);
}
print_r($new_array);
That should work for you.