Add a string to multidimensional array - php

I have a function that returns an multidimensional array() I want to concatenate a string to every value of that array. How can I do this
for example my string:
$this->$string = 'helloAddMeToArray';
and my array is:
array(array('url' => 'PleaseAddAStringToMeIAmLonely'));
So i need my array value to be like this: helloAddMeToArrayPleaseAddAStringToMeIAmLonely
I tried concatenating these with '.' but does not allow me

$oldArray = array(array('url' => 'PleaseAddAStringToMeIAmLonely'));
$newArray = array();
$this->string = 'helloAddMeToArray';
foreach($oldArray as $o) {
$newArray[] = array('url' => $this->string . $o['url']);
}

Try this:
First get string from your multidimentional Array, and type cast it.
$myString2 = (string)$myArray[0]->url;
Now use concatination: $string.$myString2;

Assuming your array could look like :
[
"key"=>[
"keyK"=>"val"
],
"key2"=>"val2"
]
and you want to concatenate a string to every value from that array you should use array_walk_recursive function .
This is a short snnipet doing this job :
$stringToConcatenate=$this->$string = 'helloAddMeToArray';
$callback($val,$key) use ($stringToConcatenate){
$val.=$val.$stringToConcatenate;
}
array_walk_recursive($youArray,$callback);
I hope it helps you .

Related

How to put PHP variable in array

$booked_seat=array(1,2);
if(in_array($seat,$booked_seat)){
$booked="red"; $book_seat="data-book='1'";
} else {
$booked=""; $book_seat="";
}
I want to put PHP variable in array
$booked_seat=array($id);
Is this possible in any way define variable in array?
You can do like this using explode() & array_shift() in php :
$str = '1,2,3';
$arr = [explode(",", $str)];
echo "<pre>";
print_r(array_shift($arr));
If you want a string instead of an array of numbers, you can use the join method http://php.net/manual/en/function.join.php.
$str = join(',', array(1, 2, 3));
If you are wanting to create an array out of a string like '1,2,3' you could use the explode method http://php.net/manual/en/function.explode.php.
$arr = explode(",", "1,2,3");

How to get value of array by using the key from a string

I have an array with some keys and I want to get the array values according to the array keys where the keys are in a string.
Example:
$arr = array(
"COV" => "Comilla Victorians",
"RK" => "Rajshaji Kings"
);
$str = "COV-RK";
Now I want to show Comilla Victorians VS Rajshaji Kings.
I can do it using some custom looping, But I need some smart coding here and looks your attention. I think there are some ways to make it with array functions that I don't know.
You could do something like:
echo implode(' VS ', array_map(function($v) use ($arr) { return $arr[$v]; }, explode('-', $str)));
So explode the string, map the resulting array, returning the value of the matching key in $arr, then just implode it.
You could try this:-
<?php
$arr = array(
"COV" => "Comilla Victorians",
"RK" => "Rajshaji Kings"
);
$str = "COV-RK";
$values = explode("-", $str); // explode string to get keys actually
echo $arr[$values[0]] . " VS " . $arr[$values[1]]; // print desired output

Turn a string into array with PHP variables

I have a string like this:
$string = '[miniTrack, boxTrack]'
I want to turn it into an array of variables like this:
$array = [$miniTrack, $boxTrack];
So that I can perform loops and iterations through the variable. And access the variables they are referencing.
I tried eval but I can't figure out how to convert it to a PHP Variable.
I KNOW THAT THIS IS NOT IDEAL, THE STRUCTURE OF THE DATABASE THIS IS PULLING FROM CAN'T BE ADJUSTED UNFORTUNATELY
Your question starts unusually, because you show an array containing a single string that is comma-separated, rather than an array of individual strings.
You could try something like the following:
$arr = [];
$string = ['miniTrack, boxtrack'];
//split the one string into an array, trimming spaces
$vars= array_map('trim', explode(',', $string[0]));
foreach($vars as $var) {
$arr[] = $var; //add the var to the array
}
print_r($arr);
Array
(
[0] => miniTrack
[1] => boxtrack
)
And if you need to create a variable for each item, you can create "variable variables":
foreach($vars as $var) {
$my_var = $$var; //variable variable
}
It should be as easy as the following:
preg_match_all('~(\w+)~','[miniTrack, boxTrack]', $matches);
foreach($matches[1] as $var)
{
print $$var;
}
You can convert your strings to array like this. It may be not ideal but you can use try this.
$string = '[miniTrack, boxTrack]';
$string = str_replace(array('[',']'),array('', '' ), $string);
$array = explode(',',$string);
Here you can iterate your array whatever you want.

How to create integer out of array?

Is it possible to convert array values into one single integer. For example, I have array with numbers
$array = array(7,4,7,2);
Is it possible to get integer value 7472 from this array?
Simple use implode as
$array = array(7,4,7,2);
echo (int)implode("",$array);// 7472
Use implode, which creates a string from an array. http://php.net/manual/en/function.implode.php
echo implode($array);
Use implode function as it create a string out of array and try this :
echo implode("",$array);
Use implode, along with (int) to convert the string result to an integer:
$a = [7,4,7,2];
$res = (int) implode('', $a);
P.S. Since PHP 5.4 you can also use the short array syntax, which replaces array() with [].
function digitsToInt($array) {
$nn = 0;
foreach ( $array as $digit) {
$nn = $nn * 10 + intval($digit);
}
return $nn;
}
var_dump( digitsToInt(array(7,4,7,2)) ); # int(7472)

Get array number from Array with String value

I have an array with string value in PHP for example : arr['apple'], arr['banana'], and many more -about 20-30 data (get it from some process). Now I want to get its value and return it to one variable.
For example, I have Original array is like this:
$arr['Apple']
$arr['Banana']
and more..
and result that I want is like this:
$arr[0] = "Apple"
$arr[1] = "Banana"
and more..
Any idea how to do that?
Why not using array_keys()?
$new_array = array_keys($array);
Use array_flip()
$new_arr = array_flip($old_arr);
Demonstration
use foreach loop
foreach($arr as $key => $val){
$new_var[] = $key;
}
use array_keys function:
$keys = array_keys($arr);
It returns an array of all the keys in array.

Categories