How to implode and keep value for each link? - php

Say I have the following code:
$arr = array('id' => $tarr = array('1' => 'Fred', '2' => 'Wilma', 'c' => 'Bam Bam'));
echo '' . implode( ', ', $tarr) . '';
This displays: Fred, Wilma, Bam Bam
but the href shows value Array instead of Fred for Fred, Wilma for Wilma etc
Cheers

You can build an output string (or array as shown here) using a foreach loop:
foreach($tarr as $v){
$out[] = "<a href='?tag=$v'>$v</a>";
}
echo implode(', ', $out)

I think what youy are trying to do is this:
$arr = array('1' => 'Fred', '2' => 'Wilma', 'c' => 'Bam Bam');
echo '';

$tarr is an array, so when it's converted to a string, it prints Array.
Don't use implode here, you should use a for loop to get each value of the array.
What you should do is something like this:
$tarr = array('1' => 'Fred', '2' => 'Wilma', 'c' => 'Bam Bam');
$aTags = array();
foreach($tarr as $v){
$aTags[] = ''.$v.'';
}
echo implode(', ', $aTags);
Also, why do you have $arr here? It's totally useless.
$arr = array('id' => $tarr = array('1' => 'Fred', '2' => 'Wilma', 'c' => 'Bam Bam'));
This is the same as:
$tarr = array('1' => 'Fred', '2' => 'Wilma', 'c' => 'Bam Bam');
$arr = array('id' => $tarr);

Related

php string to associative array with value=>value pair

I have string
$string1 = `a,b,c,d`;
$array1 = explode(',', $string1);
Gives me :
array(
(int) 0 => 'a',
(int) 1 => 'b',
(int) 2 => 'c'
(int) 3 => 'd'
)
But I want it to be like this
array(
'a' => 'a',
'b' => 'b',
'c' => 'c'
'd' => 'd'
)
How do I do that
Use array_combine function
$string = `a,b,c,d`;
$array = explode(',', $string);
var_dump(array_combine($array, $array));
I think you have to create a new array after exploding...
$tmp_arr = explode(',', $string1);
$array1 = array();
foreach ($tmp_arr as $item){
$array1[$item] = $item;
}

php merge multiple array elements remain the same, an array of different elements

Help:
Array format like this:
$arr=array(
array('element1'=>'a','element2'=>1),
array('element1'=>'b','element2'=>2),
array('element1'=>'a','element2'=>2),
array('element1'=>'b','element2'=>3),
);
Synthesis is needed,how to change it like:
$arr=array(
array('element1'=>'a','element2'=>array(1,2)),
array('element1'=>'b','element2'=>array(2,3)),
);
$new = array();
foreach ($arr as $key => $value) {
$new[$value['element1']][] = $value['element2'];
}
$new2 = array();
foreach($new as $k=>$v){
$new2[] = array('element1'=>$k,'element2'=>$v);
}
print_r($new2);
Result
array(
array('element1' => 'a', 'element2' => array(0 => 1, 1 => 2)),
array('element1' => 'b', 'element2' => array(0 => 2, 1 => 3)),
)

Make a lookup on an array literal in PHP

Making a lookup on an array is simple:
$array = array(0 => 'Zero', 1 => 'One', 2 => 'Two');
$text = $array[2]; // $text = 'Two'
But if I don't want the intermediate $array variable, is there any built in way of making a lookup an array literal?
Examples
$text = array(0 => 'Zero', 1 => 'One', 2 => 'Two')[2]; // Syntax error
$text = array_value(2, array(0 => 'Zero', 1 => 'One', 2 => 'Two')); // Unknown command
As of PHP5.5 you can do:
$text = array(0 => 'Zero', 1 => 'One', 2 => 'Two')[2];
For versions lower than 5.5 you'll have to code a function like array_value() on your own as there is no built in function for it. Like this:
function array_value($key, $array) {
if(!array_key_exists($key, $array)) {
throw new Exception('Array has no index ' . $key);
}
return $array[$key];
}
Another solution:
$array = array(0 => 'Zero', 1 => 'One', 2 => 'Two');
$pos = 2;
echo current(array_slice($array, $pos, 1));

Throw away whole array but the first item

Assume I have the following array:
$array(
'32' => array('name' => 'paul', 'age' => 43),
'17' => array('name' => 'eric', 'age' => 19),
'99' => array('name' => 'dave', 'age' => 65)
)
I am only interested in the first $array item:
$array2 = array('key'=> 32, 'name' => 'paul', 'age' => 43)
What is the most efficient way to accomplish this? In other words, can I throw out all other items of $array with one command?
Use array_shift().
array_shift() shifts the first value of the array off and returns it,
shortening the array by one element and moving everything down. All
numerical array keys will be modified to start counting from zero
while literal keys won't be touched.
$array2 = array_shift($array);
This means that $array2 now holds the first element, while $array holds the rest of the elements.
try this
$array2 = array_shift($array);
$newArr = reset($array);
I think there is no problem with that.
There are 2 options, really. Either you can just select the first item in the array
$array2 = $array[0];
Or you could use array_slice as
$array2 = array_slice($array, 0, 1);
Array_shift is probably the best way. But just for fun here is another way.
$first_element = end(array_reverse($array));
$k = array_merge(array('key' => key($array)), array_shift($array));
Returns in the specified format.
key gets you the first key, array_shift gets you the first value, and merge using array_merge
resetting an array also returns the first element (end() returns the last):
$first = reset( $array );
http://www.php.net/manual/en/function.reset.php
But to generate the exact result you want, you could write something like this
foreach( $array as $k => $first ){ // get first sub-array and its key
$first['key'] = $k; // add the key
break; // we don't care about the other elements, goodbye
}
Futuregeek's method fixed:
$first =
// returns first element, and sets it as the current element for key()
reset( $array )
// instead of array_merge, (sometimes) you can use the + operator
+
// key() will return the appropriate key after reset()
array('key' => key( $array ));
Try It :
$arr = array(
'32' => array('name' => 'paul', 'age' => 43),
'17' => array('name' => 'eric', 'age' => 19),
'99' => array('name' => 'dave', 'age' => 65)
);
foreach($arr as $key => $value)
{
$result[$key] = $value;
break;
}
print_r($result);
##-------Secount Way If you don't want Key 32--------------------------
$arr = array(
'32' => array('name' => 'paul', 'age' => 43),
'17' => array('name' => 'eric', 'age' => 19),
'99' => array('name' => 'dave', 'age' => 65)
);
$arr = array_reverse($arr);
print_r(end($arr));
#------ Third Way If you don't want Key 32 -------------
echo "<br>=======<br>";
$arr = array(
'32' => array('name' => 'paul', 'age' => 43),
'17' => array('name' => 'eric', 'age' => 19),
'99' => array('name' => 'dave', 'age' => 65)
);
$array2 = array_shift($arr);
print_r($array2);

Output an array as PHP eval-able code

Basically I want some function like array_as_php which is essentially the inverse of eval:
$array = array( '1' => 'b', '2' => 'c' );
echo array_as_php($array);
will print the following eval-able string:
array( '1' => 'b', '2' => 'c' )
var_export() is what you are looking for.
$array = array ('1' => 'b', '2' => 'c' );
echo var_export($array, true);

Categories