Foreach add to array

Ask about a PHP problem here.
Post Reply
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Foreach add to array

Post by FrederickGeek8 »

I have a function that grabs data from a url and parses it into an array. So I have one array inside another array so:
[syntax=text]ARRAY {
- First Post
-- Author
--- Fred
-- Date
--- 13/3/1998
-- URL
--- example.com
- Second Post
-- Author
--- Fred
-- Date
--- 13/3/1998
-- URL
--- example.com }[/syntax]
and I was wondering how I add an entry to each of the arrays in a foreach loop so I could get
[syntax=text]ARRAY {
-- Author
--- Fred
-- Date
--- 13/3/1998
-- URL
--- example.com
-- Shortcode
--- abc123
- Second Post
-- Author
--- Fred
-- Date
--- 13/3/1998
-- URL
--- example.com
-- Shortcode
--- abc123 }[/syntax]
Thanks!
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Foreach add to array

Post by ExtremeGaming »

Shouldn't be too hard, try this with array_slice, array_push, and array_merge :)

[syntax=php]<?php

$first_array = array_slice($your_array, 0, 6); // 6 or 7 I forget which one it will take
$second_array = array_slice($your_array, 7);

$first_array = array_push($first_array, $new_value);
$second_array = array_push($second_array, $new_value);

$new_array = array_merge($first_array, $second_array);

print_r($new_array);

?>[/syntax]

Untested.
<?php while(!$succeed = try()); ?>
Post Reply