Php Xml loop through addAttribute - php

How can I loop through addAttribute to write multiple values.
So far it looks like this:
for($i = 0; $i < 10; $i++)
{
$this->title->addAttribute('names', "'".$this->Data[$i]->names.';');
}
I get this error:
SimpleXMLElement::addAttribute(): Attribute already exists
current the xml looks like this(without the loop, with a static value name):
<button names=Tim;"/>
but I want it to look like this after the loop:
<button names=Tim;Tom;Ted"/>
how do I achieve this?

you can achieve this by first create an array with all your name, and then implode your array in your attribute
This should work
PHP
$names = [];
foreach($this->Data as $key => $value) {
$names[] = $value->names;
}
$this->title->addAttribute('names', implode(';', $names));

You do not need to use addAttribute() to add value to exist attribute. Only add new value to attribute like bottom code
for($i = 0; $i < 10; $i++)
{
$this->title['names'] .= $this->Data[$i]->names.';'
}
Check result in demo

In your code sample, you'r trying to add same attribute. Instead of it, you can create an array with names. Then you can join array elements with a glue string with implode.
$names = [];
for($i = 0; $i < 10; $i++)
{
$names[] = $this->Data[$i]->names;
}
$this->title->addAttribute('names', implode(';',$names));

Related

Foreach into PHP array

I've this these array values :
$cart_item['addons'][0]['price'] = '52';
$cart_item['addons'][1]['price'] = '34';
$cart_item['addons'][2]['price'] = '12';
......
....
I want that each values are at 0 like :
$cart_item['addons'][0]['price'] = '0';
$cart_item['addons'][1]['price'] = '0';
$cart_item['addons'][2]['price'] = '0';
....
...
So I try this code :
for ($i=0; $i > 0 ; $i++) {
$cart_item['addons'][$i]['price'] = '0';
}
But it does not work. Thanks for your help !
Try this simple solution:
$count=count($cart_item['addons']);
for($i=0; $i<$count;$i++ ){
$cart_item['addons'][$i]['price'] = '0';
}
If your array is big enough, putting count() function inside for loop is being a crazy coconut. It will be much, much slower. Please use the count outside the loop:
$count = count($cart_item['addons'])
for($i=0; $i<$count;$i++ ){
$cart_item['addons'][$i]['price'] = '0';
}
You have to loop more often to achive this:
foreach($cart_item['addons'] as &$addons {
foreach($addons as &$addon) {
$addon['price'] = 0;
}
}
You can iterate over $cart_item['addons'], like so:
foreach ($cart_item['addons'] AS $key => &$value) {
$value['price'] = 0;
}
(Your code does not get executed, because $i is never changed and so is never > 0)
Also note that you need to use the reference (&$value) when changing the array in foreach loop.
There are three parts to your for loop: for(counter | test | action){}. It might be useful for you to look at this guide about for loops. You initialise your variable:
$i = 0;
then you do a logical check (the test part):
$i > 0;
If we substitute the the variable ($i) for the value it holds (0) we get:
0 > 0
which will never be true and thus you will never get to the final part (action) of the for loop:
$i++;
Instead you could make it loop until it has moved through your entire array like this:
$elementCount = count(cart_item['addons']);
for($i=0; $i < $elementCount; $i++){
$cart_item['addons'][$i]['price'] = '0';
}
Each time it loops we add one to $i until we reach the stopping condition where $i is no longer less than the number of items in the array.
It's also worth noting that PHP has a number of functions that help working with arrays.

PHP Insert array into multidimensional array is overwrite

I´m trying to insert arrays into other array, the problem is that when I call the array_push() method it overwrites the last one element of my array, then I just get an array with data of one array (the last one):
$users_data = [];
$resultSize = count($result);
$data = $result;
for ($i = 0; $i < $resultSize; $i++) {
$person = [
'nombre' => $result[$i]['nombre'],
'apellido' => $result[$i]['apellido'],
];
array_push($users_data, $person);
// $users_data = $person; I also have tried with this method.
};
I just receive one object with this:
Object {nombre: jane, apellido: doe}
What is going wrong?
It shud be like this,
$person['nombre'][$i] = $result[$i]['nombre'];
$person['apellido'][$i] = $result[$i]['apellido'];
^ you have missed this index.
Then no need of array_push(). you can directly assign persons to user_data
Or like this :
for ($i = 0; $i < $resultSize; $i++) {
$users_data['nombre'][] = $result[$i]['nombre'];
$users_data['apellido'][] = $result[$i]['apellido'];
};

Return values of an array from behind (last element) php

say I have an array
$test_backwards=array("something1","something2","something3");
this is just a testing example and it's important to note that values will be added dinamically in my final array. so is it possible to dynamically return values from behind, namely starting from the last element?
something like this but backwards
for($i=0;$i<count($test_backwards);$i++) {
echo $test_backwards.'<br>';
}
Just start at the end and decrement your index:
for ($i = count($test_backwards) - 1; $i >= 0; $i--) {
echo $test_backwards[$i].'<br>';
}
or use array_reverse() (slower):
$test_backwards = array_reverse($test_backwards);
for ($i = 0; $i < count($test_backwards); $i++) {
echo $test_backwards[$i].'<br>';
}
You can also use array_pop(), if you do not need to keep this array. Or you can assign it to a temp array and then array_pop it, it will get and delete value from last.
$temp = $test_backwards;
while(($item = array_pop($temp)) !== NULL ) {
echo $item;
}

cannot put msSQL column names into array

I'm trying to put the column names of a msSQL table into an array. Using
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
echo mssql_field_name($result) . "<br><br>";
}
the column names print to the screen just fine. Also get_type() shows that they are strings. However, when I try to put them into an array like this:
$column_names = array();
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
$current_column = mssql_field_name($result);
array_push($column_names, $current_column);
}
var_dump($column_names); gives me an array (albeit the expected length) of boolean values. All false. I would expect to see an array containing the names of all my columns. What am I doing wrong here? Thank you
Looks like you are missing the $i argument on the mssql_field_name call. Try this maybe:
$column_names = array();
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
$column_names[] = mssql_field_name($result, $i);
}
http://php.net/manual/en/function.mssql-field-name.php

How do I create a parameter list for a function?

I am creating an array of arrays in the following way:
$final_array = array();
for($i = 0; $i < count($elements); $i++) {
for($j = 0; $j < count($elements); $j++) {
if($i!=$j)
$final_array[] = array_intersect($elements[$i], $elements[$j]);
}
}
I am trying to find out the list of elements that occur in all the arrays inside the $final_array variable. So I was wondering how to pass this to array_intersect function. Can someone tell me how to construct args using $final_array[0], $final_array[1], ... $final_array[end_value] for array_intersect? Or if there is a better approach for this, that would be great too.
I am looking for a way to construct the following:
array_intersect($final_array[0], $final_array[1], $final_array[2], ...)
Why do you need to do all of this work? Just use call_user_func_array.

Categories